JavaScript: Date.prototype

By Xah Lee. Date: . Last updated: .

Date.prototype is the value of a property key "prototype" of the Date object. 〔➤see JavaScript Date Object

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

Type of Date.prototype is object object.

// 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 JavaScript: 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

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
var 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.

var 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.

var 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”.

var 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.

var 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.

var 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().

var d = new Date("2015-04-08T15:48:22.000Z");
var 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 ( ) )

var 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()

var 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

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.