JavaScript Date Object

By Xah Lee. Date: . Last updated: .

Date is the value of a property key "Date" of the global object.

console.log ( window.hasOwnProperty ( "Date" ) ); // true

Date is a function object.

console.log ( typeof Date === "function" ); // true

The parent of Date is Function.prototype.

// parent of Date object
console.log ( Object.getPrototypeOf ( Date ) === Function.prototype ); // true

The purpose of Date is:

  1. To create a date instance. (by Date(…))
  2. Used as a namespace to hold static methods for working with date. For example, Date.parse(string).
  3. Holds the property key "prototype". The value of Date.prototype is the root parent object of all date instances. 〔➤see JavaScript: Prototype and Inheritance

Date Constructor

JavaScript: Date() Constructor

Properties


Date.prototype

JavaScript: Date.prototype

Date.now ( )

Date.now ( )

Returns the milliseconds representation of current datetime. Sample return value: 1428601360919

Same as (new Date()).getTime().

var d1 = Date.now ();
var d2 = (new Date).getTime();
console.log ( d1 ); // 1481498123423
console.log ( d2 ); // 1481498123423

ECMAScript 2015 §Numbers and Dates#sec-date.now

Date.parse ( )

Date.parse(date_time_string)

JavaScript Date.parse()

Date.UTC ( )

Date.UTC ( year, month , date , hours , minutes , seconds , ms )

Returns the milliseconds representation of current datetime. Sample return value: 1428601360919

The parameters are exactly the same as new Date(), but interprets them as UTC. 〔➤see JavaScript: Date() Constructor

The year and month arguments are required.

If less than 2 arguments are given, result is implementation dependent.

var d = Date.UTC ( 2016, 12 , 17 );
console.log ( d ); // 1484611200000
console.log ( (new Date(d)).toString() ); // Mon Jan 16 2017 16:00:00 GMT-0800 (PST)

ECMAScript 2015 §Numbers and Dates#sec-date.utc

Compute Date Range

JavaScript: Compute Date Range

Reference

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

ES5 §15.9#sec-15.9

Method Reference

  1. JavaScript: Array Methods
  2. JavaScript: String Methods
  3. JavaScript: String Methods for Regex
  4. JavaScript: RegExp Object
  5. JavaScript: Regex Syntax
  6. JavaScript Date Object
  7. JavaScript: JSON Methods
  8. JavaScript: Math Methods
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.