There are several ways to get the current time in JavaScript:
new Date()creates aDateobject representing current date/timenew Date().valueOf()returns number of milliseconds since midnight 01 January, 1970 UTCnew Date().getTime()Functionally equivalent tonew Date().valueOf()Date.now()Functionally equivalent to the 2 methods above
As mentioned in the comments and MDN links, Date.now() is not supported in Internet Explorer 8. So if IE 8 compatibility is a concern you should use new Date().valueOf() at the cost of slightly diminished code readability.
Or if you want to use Date.now() but must be compatible with browsers which don't support it, you can place following code somewhere in your JavaScript files, which will add support for it.
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
}
}
Answer from Bohuslav Burghardt on Stack OverflowThere are several ways to get the current time in JavaScript:
new Date()creates aDateobject representing current date/timenew Date().valueOf()returns number of milliseconds since midnight 01 January, 1970 UTCnew Date().getTime()Functionally equivalent tonew Date().valueOf()Date.now()Functionally equivalent to the 2 methods above
As mentioned in the comments and MDN links, Date.now() is not supported in Internet Explorer 8. So if IE 8 compatibility is a concern you should use new Date().valueOf() at the cost of slightly diminished code readability.
Or if you want to use Date.now() but must be compatible with browsers which don't support it, you can place following code somewhere in your JavaScript files, which will add support for it.
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
}
}
2019 answer
Since we no longer care about IE8, only two methods are interesting:
new Date()- creates aDateobject representing the current date/timeDate.now()- returns the number of milliseconds since midnight 01 January, 1970 UTC
(The other two methods mentioned in the older answers are functionally equivalent to Date.now() but look uglier and work in IE8.)
As a matter of style, I found it clearer to use Date.now() in code that deals with intervals (usually subtracting an earlier date from a later date to calculate the time elapsed), and new Date() for timestamps, e.g. those written to a database.
both are typically same, but Date.now() is little faster
Further Reading:
- Performance - Date.now() vs Date.getTime()
- Performance .now() vs Date.now()
I posted an answer on date and timezone related question. Answer says that the internal recording of the date generated on client side are at zero timezone offset. Just adding answers to individual questions:
My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.
Yes
This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?
Yes
Is there any difference between a UTC timestamp and timestamp given by Date.now()?
No
From the specs:
When
Dateis called as a function rather than as a constructor, it returns a String representing the current time (UTC).
and:
When
Dateis called as part of anewexpression, it is a constructor: it initialises the newly created object.
So, new Date(...) returns an object such that obj instanceof Date is true, whereas Date(...) basically returns the same as new Date().toString().
new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.
These things are the same (edit semantically; performance is a little better with .now()):
var t1 = Date.now();
var t2 = new Date().getTime();
However, the time value from any already-created Date instance is frozen at the time of its construction (or at whatever time/date it's been set to). That is, if you do this:
var now = new Date();
and then wait a while, a subsequent call to now.getTime() will tell the time at the point the variable was set.
They are effectively equivalent, but you should use Date.now(). It's clearer and about twice as fast.
Edit: Source: https://jsperf.app/nimime/15
Did my best to google around and found nothing. Anyone have insight? They seem the same but figured it was worth asking.
Redditors, Can someone please explain why (what is the reasoning/advantage)
below have direct methods
Date.UTC() Date.now() Date.parse()
And below have prototype methods
Date.prototype.getDate() Date.prototype.getDay()
For testing:
const functionName = fn => (console.log(fn.name), fn); functionName(Date.prototype.getDate); // logs getDate functionName(Date.getDate); // logs Cannot read property 'name' of undefined functionName(Date.now); // logs now