Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date = new Date(); date.valueOf() vs Date.now()

Tags:

javascript

People also ask

What is the difference between Date now () and new Date ()?

new Date() - creates a Date object representing the current date/time. Date. now() - returns the number of milliseconds since midnight 01 January, 1970 UTC.

What is the value of new Date ()?

For example, new Date(2020, 5, 0) will return May 31st, 2020. Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999 . All other values are the actual year.

What does new Date () return?

"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

Is new Date () async?

log("Now: " + (new Date()). toString()); It's a bit confusing because this piece of code look like a synchronous code: no callback appears in the code. But in fact, it is asynchronous.


There are several ways to get the current time in JavaScript:

  • new Date() creates a Date object representing current date/time
  • new Date().valueOf() returns number of milliseconds since midnight 01 January, 1970 UTC
  • new Date().getTime() Functionally equivalent to new 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 a Date object representing the current date/time
  • Date.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.


After reading MDN, it seems new Date().getTime(), new Date().valueof() and Date.now() are all functionally equivalent. However, Date.now() is only supported in browsers from different versions, most importantly IE9. So if pre IE9 support is important, you may need to polyfill.

Edit: really, the only concern is IE. Isn't it always? ;)


I am usually using +new Date() or +new Date().toString(36) When i need timestamps.

Recently i changed to Date.now() because it is more efficient or will become one day more efficient.

Technically the os reports the date time as a primitive, by calling new Date() the javascript engine must create a object with prototypes (which is passed by reference, and must be garbage collected later), and then do another function call of valueOf.

Date.now could be a static function of the global Date object, that does simple math on the value reported from the OS

I did'nt checked the source code of all javascript engines to make sure that Date.now does'nt create a date instance, but it is more efficient or will become one day more efficient.