- Get the current time in milliseconds using JavaScript
- Date.prototype.getTime()
- Try it
- Syntax
- Return value
- Description
- Reduced time precision
- Examples
- Using getTime() for copying dates
- Measuring execution time
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Date.now()
- Try it
- Syntax
- Return value
- Description
- Reduced time precision
- Examples
- Measuring time elapsed
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Date.prototype.getMilliseconds()
- Try it
- Syntax
- Return value
- Examples
- Using getMilliseconds()
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
Get the current time in milliseconds using JavaScript
In this tutorial, we are going to learn about how to get the current time in milliseconds using JavaScript.
In JavaScript, we can get the current by calling a getTime() on the new Date() constructor.
const currentTime = new Date().getTime();
To get the current time in milliseconds, divide the current time with 1000 , because 1second = 1000 milliseconds.
const currentTime = new Date().getTime(); const milliseconds = currentTime/1000; console.log(milliseconds);
The / operator in the above code returns the result of dividing the first operand by the second.
Similarly, we can also use the Date.now() method to get the current time in Milliseconds.
const milliseconds = Date.now(); console.log(milliseconds);
The Date.now() method returns the current time in milliseconds since the epoch (January 1, 1970).
Date.prototype.getTime()
The getTime() method of Date instances returns the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
Try it
Syntax
Return value
A number representing the timestamp, in milliseconds, of this date. Returns NaN if the date is invalid.
Description
Date objects are fundamentally represented by a timestamp, and this method allows you to retrieve the timestamp. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.
Reduced time precision
To offer protection against timing attacks and fingerprinting, the precision of new Date().getTime() might get rounded depending on browser settings. In Firefox, the privacy.reduceTimerPrecision preference is enabled by default and defaults to 2ms. You can also enable privacy.resistFingerprinting , in which case the precision will be 100ms or the value of privacy.resistFingerprinting.reduceTimerPrecision.microseconds , whichever is larger.
// reduced time precision (2ms) in Firefox 60 new Date().getTime(); // 1519211809934 // 1519211810362 // 1519211811670 // … // reduced time precision with `privacy.resistFingerprinting` enabled new Date().getTime(); // 1519129853500 // 1519129858900 // 1519129864400 // …
Examples
Using getTime() for copying dates
Constructing a date object with the identical time value.
// Since month is zero based, birthday will be January 10, 1995 const birthday = new Date(1994, 12, 10); const copy = new Date(); copy.setTime(birthday.getTime());
Measuring execution time
Subtracting two subsequent getTime() calls on newly generated Date objects, give the time span between these two calls. This can be used to calculate the executing time of some operations. See also Date.now() to prevent instantiating unnecessary Date objects.
let end, start; start = new Date(); for (let i = 0; i 1000; i++) Math.sqrt(i); > end = new Date(); console.log(`Operation took $end.getTime() - start.getTime()> msec`);
Note: In browsers that support the Web Performance API’s high-resolution time feature, Performance.now() can provide more reliable and precise measurements of elapsed time than Date.now() .
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 1, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Date.now()
The Date.now() static method returns the number of milliseconds elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
Try it
Syntax
Return value
A number representing the timestamp, in milliseconds, of the current time.
Description
Reduced time precision
To offer protection against timing attacks and fingerprinting, the precision of Date.now() might get rounded depending on browser settings. In Firefox, the privacy.reduceTimerPrecision preference is enabled by default and defaults to 2ms. You can also enable privacy.resistFingerprinting , in which case the precision will be 100ms or the value of privacy.resistFingerprinting.reduceTimerPrecision.microseconds , whichever is larger.
// reduced time precision (2ms) in Firefox 60 Date.now(); // 1519211809934 // 1519211810362 // 1519211811670 // … // reduced time precision with `privacy.resistFingerprinting` enabled Date.now(); // 1519129853500 // 1519129858900 // 1519129864400 // …
Examples
Measuring time elapsed
You can use Date.now() to get the current time in milliseconds, then subtract a previous time to find out how much time elapsed between the two calls.
const start = Date.now(); doSomeLongRunningProcess(); console.log(`Time elapsed: $Date.now() - start> ms`);
For more complex scenarios, you may want to use the performance API instead.
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 1, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Date.prototype.getMilliseconds()
The getMilliseconds() method of Date instances returns the milliseconds for this date according to local time.
Try it
Syntax
Return value
An integer, between 0 and 999, representing the milliseconds for the given date according to local time. Returns NaN if the date is invalid.
Examples
Using getMilliseconds()
The milliseconds variable has value 0 , based on the value of the Date object xmas95 , which doesn’t specify the milliseconds component, so it defaults to 0.
const xmas95 = new Date("1995-12-25T23:15:30"); const milliseconds = xmas95.getMilliseconds(); console.log(milliseconds); // 0
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 1, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.