JavaScript TutorialDatatypes in JavaScriptEvaluating JavaScriptFunctional JavaScriptJavaScript .postMessage() and MessageEventJavaScript AJAXJavaScript Anti-patternsJavaScript Arithmetic (Math)JavaScript ArraysJavaScript Arrow FunctionsJavaScript Async functions (async/await)JavaScript Async IteratorsJavaScript Automatic Semicolon Insertion - ASIJavaScript Battery Status APIJavaScript Behavioral Design PatternsJavaScript Binary DataJavaScript Bitwise operatorsJavaScript Bitwise Operators - Real World Examples (snippets)JavaScript BOM (Browser Object Model)JavaScript Built-in ConstantsJavaScript CallbacksJavaScript ClassesJavaScript CommentsJavaScript Comparison OperationsJavaScript ConditionsJavaScript ConsoleJavaScript Constructor functionsJavaScript Context (this)JavaScript CookiesJavaScript Creational Design PatternsJavaScript Custom ElementsJavaScript Data attributesJavaScript Data ManipulationJavaScript DateJavaScript Date ComparisonJavaScript DebuggingJavaScript Declarations and AssignmentsJavaScript Destructuring assignmentJavaScript Detecting browserJavaScript EnumerationsJavaScript Error HandlingJavaScript Escape SequencesJavaScript EventsJavaScript execCommand and contenteditableJavaScript FetchJavaScript File API, Blobs and FileReadersJavaScript Fluent APIJavaScript FunctionsJavaScript GeneratorsJavaScript GeolocationJavaScript Global error handling in browsersJavaScript HistoryJavaScript How to make iterator usable inside async callback functionJavaScript IndexedDBJavaScript InheritanceJavaScript Intervals and TimeoutsJavaScript JSONJavaScript Linters - Ensuring code qualityJavaScript LocalizationJavaScript LoopsJavaScript MapJavaScript Memory efficiencyJavaScript Method ChainingJavaScript Modals - PromptsJavaScript Modularization TechniquesJavaScript ModulesJavaScript NamespacingJavaScript Navigator ObjectJavaScript Notifications APIJavaScript ObjectsJavaScript Performance TipsJavaScript PromisesJavaScript Prototypes, objectsJavaScript ProxyJavaScript Regular expressionsJavaScript requestAnimationFrameJavaScript Reserved KeywordsJavaScript Same Origin Policy & Cross-Origin CommunicationJavaScript ScopeJavaScript ScreenJavaScript Security issuesJavaScript Selection APIJavaScript Server-sent eventsJavaScript SetJavaScript Setters and GettersJavaScript Strict modeJavaScript StringsJavaScript SymbolsJavaScript Tail Call OptimizationJavaScript Template Literals

JavaScript Timestamps

From WikiOD

Syntax[edit | edit source]

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

Remarks[edit | edit source] is available in modern web browsers and provides reliable timestamps with sub-millisecond resolution.

Since 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[edit | edit source] 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 =;

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

Low-resolution timestamps[edit | edit source] returns the number of whole milliseconds that have elapsed since 1 January 1970 00:00:00 UTC.

t =;

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

Get Timestamp in Seconds[edit | edit source]

To get the timestamp in seconds

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

Support for legacy browsers[edit | edit source]

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

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

Or, to provide a function for use in older browsers, use this polyfill:

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