JS: Date.prototype

By Xah Lee. Date: . Last updated: .

Date.prototype is the value of the property key "prototype" of the function Date.

[see JS: Date Object]

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

Type of Date.prototype is object.

[see JS: Data Types]

// type of Date.prototype
console.log ( typeof Date.prototype === "object" ); // true
console.log ( Object.prototype.toString.call( Date.prototype ) === "[object Object]" ) // true

Parent of Date.prototype is Object.prototype.

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

[see JS: Prototype and Inheritance]

Date.prototype is the parent of all date instances.

console.log ( Object.getPrototypeOf ( new Date() ) === Date.prototype ); // true

The purpose of Date.prototype object is to provides methods and properties useful for all date instances.

Properties


constructor

Date.prototype.constructor

Value is the Date function object.

[see JS: Date Object]

[see JS: Property Key "constructor"]

console.log ( Date.prototype.constructor === Date ); // true
console.log ( (new Date()).constructor === Date ); // true

Date Object Get Methods

Most date get/set methods have 2 versions. One returns local time info, one returns UTC time info. For example, getHours() vs getUTCHours().

// example of using Date get methods
let dt = new Date("2017-06-13T21:00:01.000Z");
console.log( dt.getFullYear() ); // 2017
console.log( dt.getUTCMonth() ); // 5
console.log( dt.getUTCDate() ); // 13
console.log( dt.getUTCDay() ); // 2 (Tuesday)
console.log( dt.getUTCHours() ); // 21
console.log( dt.getUTCMinutes() ); // 0
console.log( dt.getUTCSeconds() ); // 1

What is the difference between UTC and GMT?

For practical purposes, they are the same.

UTC (Coordinated Universal Time) is a time standard based on atomic clock.

GMT (Greenwich Mean Time) is a older time standard.

“Zulu time” is a term used in military and is the same as GMT. (GMT and UTC has an abbrev of Z, because Z mean zero, for zero offset. Z in NATO phonetic alphabet is read as Zulu. That's why GMT/UTC is sometimes said “Zulu time”.)


getTime() → returns the millisecond representation of a Date object. Sample return value: 1428605303481.

getFullYear(),
getUTCFullYear() → year of the date. 4 digits. For example, 2014.

getYear() → returns the year minus 1901. Deprecated. Use getFullYear().

console.log( (new Date("1999")).getYear() ) // 98
console.log( (new Date("2014")).getYear() ) // 113

getMonth(),
getUTCMonth() → month. 0 to 11. January is 0. February is 1. December is 11.

getDate(),
getUTCDate() → day of the month. 1 to 31.

getDay(),
getUTCDay() → day of the week. 0 to 6. Sunday is 0. Monday is 1. Saturday is 6.

getHours(),
getUTCHours() → hours. 0 to 23.

getMinutes(),
getUTCMinutes() → minutes. 0 to 59.

getSeconds(),
getUTCSeconds() → seconds. 0 to 59.

getMilliseconds(),
getUTCMilliseconds() → milliseconds field of a Date object. 0 to 999.

getTimezoneOffset() → Difference, in minutes, between the local and UTC time of this date. Daylight saving time is taken account of.

Date Object Set Methods

setTime(milliseconds) → Sets the date object to a specific datetime of milliseconds, the internal date format.

setFullYear(year,month,date),
setUTCFullYear(…) → Set the year to year. The other parameters are optional.

setYear(year) → Sets the year to year. Deprecated. Use setFullYear().

setMonth(month,date),
setUTCMonth(…) → Sets the month to month. Note, January is 0. Other parameters are optional.

setDate(date),
setUTCDate(…) → Sets the date part to date.

setHours(hour, min, sec, millisecond),
setUTCHours(…) → Sets the hour to hour. The rest parameters are optional.

setMinutes(min, sec, millisecond),
setUTCMinutes(…) → Sets the minutes to min. The rest parameters are optional.

setSeconds(sec, millisecond),
setUTCSeconds(…) → Sets the second sec. The rest parameters are optional.

setMilliseconds(ms),
setUTCMilliseconds(…) → Sets the milliseconds part of a date to ms.

Methods for Printing Date

Print to Local Formats

toLocaleString() → Returns a string that is the complete date info in human readable form of the local time zone, and in a format of local convention.

let dt = new Date("2017-04-08T15:48:22.000Z");
console.log( dt.toLocaleString() ); // 4/8/2017, 8:48:22 AM
console.log( dt.toLocaleDateString() ); // 4/8/2017
console.log( dt.toLocaleTimeString() ); // 8:48:22 AM

toLocaleDateString() → Same as toLocaleString(), but return only the date part.

toLocaleTimeString() → Same as toLocaleString(), but return only the time part.

Print Datetime Portion

toDateString() → Returns a string that is the date part in human readable form of the local time zone.

toTimeString() → Returns a string that is the time part in human readable form of the local time zone.

let d = new Date("2015-04-08T15:48:22.000Z");
console.log( d.toDateString() ); // Wed Apr 08 2015
console.log( d.toTimeString() ); // 08:48:22 GMT-0700 (PDT)

Print Datetime in International Formats

toISOString() → Returns a string that is ISO-8601 format in UTC timezone. Sample output: “2015-04-08T15:48:22.000Z”.

let d = new Date("2015-04-08T15:48:22.000Z");
console.log( d.toISOString() ); // 2015-04-08T15:48:22.000Z

toUTCString() → Returns human readable format in UTC timezone. Return string format is implementation dependent.

let d = new Date("2015-04-08T15:48:22.000Z");
console.log( d.toUTCString() ); // Wed, 08 Apr 2015 15:48:22 GMT

toGMTString() → Returns a string using the GMT time zone. Deprecated. Use toUTCString() instead.

let d = new Date("2015-04-08T15:48:22.000Z");
console.log( d.toGMTString() ); // Wed, 08 Apr 2015 15:48:22 GMT

toJSON() → Returns a JSON representation of Date object, using toISOString().

let d = new Date("2015-04-08T15:48:22.000Z");
let j = d.toJSON();
console.log( j ); // 2015-04-08T15:48:22.000Z
console.log( typeof j === "string" ); // true

toString() → Returns date time in string format. This function is generic.

if d is a date instance with milliseconds part 0, then the following is guaranteed to be true, by spec: Date.parse ( d.toString ( ) ) === d.valueOf ( ) )

let d = new Date("2015-04-08T15:48:22.000Z");
console.log( d.toString() ); // Wed Apr 08 2015 08:48:22 GMT-0700 (PDT)
console.log ( Date.parse ( d.toString ( ) ) === d.valueOf ( ) ); // true

valueOf

valueOf() → Returns the millisecond representation of a date time. Sample return value: 1428605303481. Same as getTime()

let d = new Date();
console.log ( d.valueOf ( ) ); // 1482029397281
console.log ( d.getTime ( ) ); // 1482029397281
console.log ( d.valueOf ( ) === d.getTime ( ) ); // true

Reference

ECMAScript 2015 §Numbers and Dates#sec-properties-of-the-date-prototype-object

ECMAScript 5.1 §15.9#sec-15.9.5

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