JS: Date.prototype

By Xah Lee. Date: . Last updated: .

What is Date.prototype

Date.prototype is the value of the property key "prototype" of the function Date. [see Date Object]

Date.hasOwnProperty ( "prototype" ) 

Type

Type of Date.prototype is Object .

typeof Date.prototype === "object"

Parent

Parent of Date.prototype is Object.prototype .

Reflect.getPrototypeOf ( Date.prototype ) === Object.prototype 

Purpose

Date.prototype is the parent of all date instances.

Reflect.getPrototypeOf ( new Date() ) === Date.prototype 

Purpose of Date.prototype is to provide methods and properties useful for all date instances.

Properties

Get

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

getTime()
Return the millisecond representation of a Date object. Sample: 1428605303481.
getFullYear(), getUTCFullYear()
Year of the date. 4 digits. e.g. 2014.
getYear()
Return the year minus 1901. Deprecated. Use getFullYear().
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.

Set

setTime(milliseconds)
Set 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)
Set the year to year. Deprecated. Use setFullYear().
setMonth(month, date), setUTCMonth()
Set the month to month. Note, January is 0. Other parameters are optional.
setDate(date), setUTCDate()
Set the date part to date.
setHours(hour, min, sec, millisecond), setUTCHours()
Set the hour to hour. The rest parameters are optional.
setMinutes(min, sec, millisecond), setUTCMinutes()
Set the minutes to min. The rest parameters are optional.
setSeconds(sec, millisecond), setUTCSeconds()
Set the second sec. The rest parameters are optional.
setMilliseconds(ms), setUTCMilliseconds()
Set the milliseconds part of a date to ms.

Print

toLocaleString()
Return a string that is the complete date info in human readable form of the local time zone, and in a format of local convention. Sample:
"Sat Apr 08 2017 08:48:22 GMT-0700 (PDT)"
toLocaleDateString()
Same as toLocaleString(), but return only the date part. Sample:
"Sat Apr 08 2017"
toLocaleTimeString()
Same as toLocaleString(), but return only the time part. Sample:
"08:48:22 GMT-0700 (PDT)"
toDateString()
Return a string that is the date part in human readable form of the local time zone. Sample:
"Wed Apr 08 2015"
toTimeString()
Return a string that is the time part in human readable form of the local time zone. Sample:
"08:48:22 GMT-0700 (PDT)"
toISOString()
Return a string that is ISO-8601 format in UTC timezone. Sample:
"2015-04-08T15:48:22.000Z"
toUTCString()
Return human readable format in UTC timezone. Return string format is implementation dependent. Sample:
"Wed, 08 Apr 2015 15:48:22 GMT"
toGMTString()
Return a string using the GMT time zone. Deprecated. Use toUTCString() instead. Sample:
"Wed, 08 Apr 2015 15:48:22 GMT"
toJSON()
Return a JSON representation of Date object, using toISOString(). Sample:
"2015-04-08T15:48:22.000Z"
toString()
Return date time in string format. Sample:
"Wed Apr 08 2015 08:48:22 GMT-0700 (PDT)"
valueOf()
Return the millisecond representation of a date time. Sample:
1428605303481. Same as getTime()

Misc

Date.prototype.constructor
Value is Date. [see Date Object]
[ Symbol.toPrimitive ] (JS2015)

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

JavaScript, Date

BUY ΣJS JavaScript in Depth