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 OverflowVideos
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();
}
}
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.