JS: Date Constructor

By Xah Lee. Date: . Last updated: .

The Date constructor can be called as a function or with new

Date()

Date()

Returns a string that represents the current time when this function is called.

Sample return value: "Sun Dec 11 2016 15:47:24 GMT-0800 (PST)"

Arguments to Date() are ignored.

Date() is equivalent to (new Date()).toString().

var d1 = Date();
console.log( d1 ); // Thu Apr 13 2017 22:38:05 GMT-0700 (PDT)
var d1 = Date();
var d2 = (new Date()).toString();

console.log( d1 ); // Sun Dec 11 2016 15:47:24 GMT-0800 (PST)
console.log( d2 ); // Sun Dec 11 2016 15:47:24 GMT-0800 (PST)

console.log ( d1 === d2 ); // true

// return value is string type
console.log( typeof d1 === "string" ); // true
// arg to Date() are ignored

console.log( Date(1999) );
// Thu Apr 13 2017 22:39:32 GMT-0700 (PDT)

console.log( Date(1428605303481) );
// Thu Apr 13 2017 22:39:32 GMT-0700 (PDT)

console.log( Date() );
// Thu Apr 13 2017 22:39:32 GMT-0700 (PDT)

new Date()

new Date() → Returns a date object that represents the current datetime when this function is called.

var d = new Date();
console.log ( d.toString ( ) ); // Sun Dec 18 2016 08:33:07 GMT-0800 (PST)

new Date(2016, 0 …)

new Date(year, month)

new Date(year, month, day, hours, minutes, seconds, ms)

Returns a date object of given (local) datetime.

The year, month are required, other parameters are optional.

The arguments are interpreted as local time. (If you want args to be UTC, use static method Date.UTC(…).)

  1. year → 4 digits integer.
  2. month → 0 to 11. January is 0.
  3. day → 1 to 31.
  4. hours → 0 to 23.
  5. minutes → 0 to 59.
  6. seconds → 0 to 59.
  7. ms → 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(1428605303481)

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

This milliseconds is how JavaScript represents datetime internally.

This is the same value returned by datetime.getTime() method.

var d = new Date(1428605303481);
console.log ( d.toString () ); // Thu Apr 09 2015 11:48:23 GMT-0700 (PDT)
console.log ( d.getTime() ); // 1428605303481

new Date("2015-04-08T15:48:22.000Z")

new Date(date_time_string) → Returns a date object that represents the datetime in date_time_string.

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

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

var d = new Date("2016-04-08T15:48:22.000Z");
console.log( d.toString() ); // Fri Apr 08 2016 08:48:22 GMT-0700 (PDT)

For the format of date_time_string, see JavaScript Date.parse

Reference

ECMAScript 2015 §Numbers and Dates#sec-date-constructor

Date Topic

  1. JS: Date Tutorial
  2. JavaScript Date.parse
  3. JS: Compute Date Range

  1. JS: Date Object
  2. JS: Date Constructor
  3. JS: Date.prototype

JS Object Reference

  1. Global
  2. Object
  3. Array
  4. Function
  5. String
  6. RegExp
  7. Date
  8. Set
  9. Map
  10. JSON
  11. Math
  12. Reflect
  13. Number
  14. Boolean
  15. Symbol
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth