JavaScript: Date() Constructor

By Xah Lee. Date: . Last updated: .

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

They have different behavior.

Date()

Date() → Returns a string that represents the current time when this function is called. Any arguments are ignored.

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

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

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(345) ); // Sun Dec 11 2016 15:47:24 GMT-0800 (PST)

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) returns 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

ES5 §15.9#sec-15.9.2

Date Topic

  1. JavaScript: Date Tutorial
  2. JavaScript: Date Object
  3. JavaScript: Date.prototype
  4. JavaScript Date.parse()
  5. JavaScript: Date() Constructor
  6. JavaScript: Compute Date Range

Object Reference

  1. JavaScript: Object Object
  2. JavaScript: Array Object
  3. JavaScript: Function Object
  4. JavaScript: String Object
  5. JavaScript: RegExp.prototype
  6. JavaScript: Date Object
  7. JavaScript: Set Object
  8. JavaScript: Map Object
  9. JavaScript: JSON Object
  10. JavaScript: Math Object
  11. JavaScript: Reflect Object
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.