Getting started with JavaScriptFunctionsArraysObjectsAJAXClassesArithmetic (Math)Comparison OperationsConditionsLoopsPromisesRegular expressionsDateError HandlingGeolocationCookiesIntervals and TimeoutsGeneratorsHistoryStrict modeCustom ElementsJSONBinary DataTemplate LiteralsWeb StorageFetchScopeModulesScreenInheritanceTimestampsDestructuring assignmentWorkersVariable coercion/conversionDebuggingNotifications APIBuilt-in ConstantsWebSocketsWeb Cryptography APIAsync functions (async/await)StringsConstructor functionsexecCommand and contenteditablePerformance TipsMapCreational Design PatternsrequestAnimationFrameReserved KeywordsMethod ChainingGlobal error handling in browsersUnary OperatorsFile API, Blobs and FileReadersCommentsConsoleTail Call OptimizationDetecting browserEnumerationsSymbolsLocalizationSelection APICallbacksSetDeclarations and AssignmentsFunctional JavaScriptModals - PromptsData attributesThe Event LoopBattery Status APIData ManipulationBitwise operatorsTranspilingBOM (Browser Object Model)Unit Testing JavascriptLinters - Ensuring code qualityAutomatic Semicolon Insertion - ASIIndexedDBAnti-patternsNavigator ObjectModularization TechniquesProxySame Origin Policy & Cross-Origin CommunicationArrow Functions.postMessage() and MessageEventWeakMapWeakSetEscape SequencesBehavioral Design PatternsServer-sent eventsAsync IteratorsNamespacingEvaluating JavaScriptMemory efficiencyDate ComparisonHow to make iterator usable inside async callback functionContext (this)Setters and GettersVibration APIPrototypes, objectsDatatypes in JavascriptBitwise Operators - Real World Examples (snippets)Fluent APITilde ~Security issuesUsing javascript to get/set CSS custom variablesJavaScript VariablesEvents

Timestamps

Other topics

Remarks:

performance.now() is available in modern web browsers and provides reliable timestamps with sub-millisecond resolution.

Since Date.now() and (new Date()).getTime() are based on the system time, they often get skewed by a few milliseconds when the system time is automatically synchronized.

High-resolution timestamps

performance.now() returns a precise timestamp: The number of milliseconds, including microseconds, since the current web page started to load.

More generally, it returns the time elapsed since the performanceTiming.navigationStart event.

t = performance.now();

For example, in a web browser's main context, performance.now() returns 6288.319 if the web page began to load 6288 milliseconds and 319 microseconds ago.

Low-resolution timestamps

Date.now() returns the number of whole milliseconds that have elapsed since 1 January 1970 00:00:00 UTC.

t = Date.now();

For example, Date.now() returns 1461069314 if it was called on 19 April 2016 at 12:35:14 GMT.

Support for legacy browsers

In older browsers where Date.now() is unavailable, use (new Date()).getTime() instead:

t = (new Date()).getTime();

Or, to provide a Date.now() function for use in older browsers, use this polyfill:

if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}

Get Timestamp in Seconds

To get the timestamp in seconds

Math.floor((new Date().getTime()) / 1000)

Syntax:

  • millisecondsAndMicrosecondsSincePageLoad = performance.now();
  • millisecondsSinceYear1970 = Date.now();
  • millisecondsSinceYear1970 = (new Date()).getTime();

Contributors

Topic Id: 606

Example Ids: 1971,1972,1973,4897

This site is not affiliated with any of the contributors.