JS: Date Constructor

By Xah Lee. Date: . Last updated: .
Return a date string. Same as (new Date()).toString()
new Date()
Return a date object.


Return a string that represents the current time when this function is called. Sample return value:
"Tue May 22 2018 18:58:08 GMT-0700 (PDT)"
Arguments to Date() are ignored.
// Sun Sep 06 2020 16:11:39 GMT-0700 (PDT)
Date() === (new Date()).toString()
// arg to Date() are ignored
console.log(Date(1999) === Date());

new Date()

new Date()
Return a date object that represents the current datetime when this function is called.
const d = new Date();
// Tue May 22 2018 18:58:08 GMT-0700 (PDT)

new Date(year, month …)

new Date(year, month)
Return a date object of given (local) datetime.
new Date(year, month, day, hours, minutes, seconds, ms)
The year, month are required, other parameters are optional.

The arguments are interpreted as local time. (If you want args to be UTC, use Date.UTC(). [see Date.UTC] )

Argument format:

4 digits integer.
0 to 11. January is 0.
1 to 31.
0 to 23.
0 to 59.
0 to 59.
0 to 999.
// note: January is 0
console.log((new Date(2016, 0, 1)).toUTCString());
// Fri, 01 Jan 2016 08:00:00 GMT

new Date(milliseconds)

new Date(milliseconds)
Return a date object that represents the datetime of milliseconds since 01 January, 1970 UTC.

This milliseconds is how JavaScript represents datetime internally.

To get milliseconds from date object, use datetime.getTime().

const ms = 1428605303481;

  ms === (new Date(ms)).getTime(),
); // true

new Date(date_time_string)

new Date(DateTimeStr)
Return a date object that represents the datetime in DateTimeStr.

Note: new Date(DateTimeStr) is equivalent to new Date( Date.parse(DateTimeStr ) )

The Date.parse(DateTimeStr) return milliseconds, then the new Date(milliseconds) turns that into a date instance.

  (new Date("2016-04-08T15:48:22.000Z")).toString() ===
    "Fri Apr 08 2016 08:48:22 GMT-0700 (PDT)",

For the format of DateTimeStr, see Date.parse

JavaScript, Date

BUY ΣJS JavaScript in Depth