Creates a JavaScript Date
instance that represents a single moment in time. Date
objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC.
new Date(); new Date(value); new Date(dateString); new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
Note: JavaScript Date
objects can only be instantiated by calling JavaScript Date
as a constructor: calling it as a regular function (i.e. without the new
operator) will return a string rather than a Date
object; unlike other JavaScript object types, JavaScript Date
objects have no literal syntax.
Note: The argument monthIndex
is 0-based. This means that January = 0
and December = 11
.
Note: Where Date
is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1)
is equivalent to new Date(2014, 1, 1)
, both create a date for 2014-02-01
(note that the month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0, 70)
is equivalent to new Date(2013, 2, 1, 1, 10)
which both create a date for 2013-03-01T01:10:00
.
Note: Where Date
is called as a constructor with more than one argument, the specified arguments represent local time. If UTC is desired, use new Date(
with the same arguments.Date.UTC(...)
)
value
dateString
Date.parse()
method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). Note: parsing of date strings with the Date
constructor (and Date.parse
, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
year
monthIndex
day
Optional
hours
Optional
minutes
Optional
seconds
Optional
milliseconds
Optional
Date
object for the current date and time according to system settings for timezone offset.Date
object range is -100,000,000 days to 100,000,000 days relative to January 1, 1970 UTC.Date
object provides uniform behavior across platforms. The time value can be passed between systems to create a date that represents the same moment in time.Date
object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.Date
as a function (i.e., without the new
operator) will return a string representing the current date and time.Date.prototype
Date
object.Date.length
Date.length
is 7. This is the number of arguments handled by the constructor.Date.now()
Date.parse()
Note: Parsing of strings with Date.parse
is strongly discouraged due to browser differences and inconsistencies.
Date.UTC()
Date
instancesAll Date
instances inherit from Date.prototype
. The prototype object of the Date
constructor can be modified to affect all Date
instances.
Date.prototype.getDate()
Date.prototype.getDay()
Date.prototype.getFullYear()
Date.prototype.getHours()
Date.prototype.getMilliseconds()
Date.prototype.getMinutes()
Date.prototype.getMonth()
Date.prototype.getSeconds()
Date.prototype.getTime()
Date.prototype.getTimezoneOffset()
Date.prototype.getUTCDate()
Date.prototype.getUTCDay()
Date.prototype.getUTCFullYear()
Date.prototype.getUTCHours()
Date.prototype.getUTCMilliseconds()
Date.prototype.getUTCMinutes()
Date.prototype.getUTCMonth()
Date.prototype.getUTCSeconds()
Date.prototype.getYear()
getFullYear()
instead.Date.prototype.setDate()
Date.prototype.setFullYear()
Date.prototype.setHours()
Date.prototype.setMilliseconds()
Date.prototype.setMinutes()
Date.prototype.setMonth()
Date.prototype.setSeconds()
Date.prototype.setTime()
Date
object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC, allowing for negative numbers for times prior.Date.prototype.setUTCDate()
Date.prototype.setUTCFullYear()
Date.prototype.setUTCHours()
Date.prototype.setUTCMilliseconds()
Date.prototype.setUTCMinutes()
Date.prototype.setUTCMonth()
Date.prototype.setUTCSeconds()
Date.prototype.setYear()
setFullYear()
instead.Date.prototype.toDateString()
Date
as a human-readable string like 'Thu Apr 12 2018'Date.prototype.toISOString()
Date.prototype.toJSON()
Date
using toISOString()
. Intended for use by JSON.stringify()
.Date.prototype.toGMTString()
Date
based on the GMT (UT) time zone. Use toUTCString()
instead.Date.prototype.toLocaleDateString()
Date.prototype.toLocaleFormat()
Date.prototype.toLocaleString()
Object.prototype.toLocaleString()
method.Date.prototype.toLocaleTimeString()
Date.prototype.toSource()
Date
object; you can use this value to create a new object. Overrides the Object.prototype.toSource()
method.Date.prototype.toString()
Date
object. Overrides the Object.prototype.toString()
method.Date.prototype.toTimeString()
Date
as a human-readable string.Date.prototype.toUTCString()
Date.prototype.valueOf()
Date
object. Overrides the Object.prototype.valueOf()
method.Date
objectThe following examples show several ways to create JavaScript dates:
Note: parsing of date strings with the Date
constructor (and Date.parse
, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.
var today = new Date(); var birthday = new Date('December 17, 1995 03:24:00'); var birthday = new Date('1995-12-17T03:24:00'); var birthday = new Date(1995, 11, 17); var birthday = new Date(1995, 11, 17, 3, 24, 0);
In order to create and get dates between the years 0 and 99 the Date.prototype.setFullYear()
and Date.prototype.getFullYear()
methods should be used.
var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT) // Deprecated method, 98 maps to 1998 here as well date.setYear(98); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT) date.setFullYear(98); // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)
The following examples show how to determine the elapsed time between two JavaScript dates in milliseconds.
Due to the differing lengths of days (due to daylight saving changeover), months and years, expressing elapsed time in units greater than hours, minutes and seconds requires addressing a number of issues and should be thoroughly researched before being attempted.
// using Date objects var start = Date.now(); // the event to time goes here: doSomethingForALongTime(); var end = Date.now(); var elapsed = end - start; // elapsed time in milliseconds
// using built-in methods var start = new Date(); // the event to time goes here: doSomethingForALongTime(); var end = new Date(); var elapsed = end.getTime() - start.getTime(); // elapsed time in milliseconds
// to test a function and get back its return function printElapsedTime(fTest) { var nStartTime = Date.now(), vReturn = fTest(), nEndTime = Date.now(); console.log('Elapsed time: ' + String(nEndTime - nStartTime) + ' milliseconds'); return vReturn; } var yourFunctionReturn = printElapsedTime(yourFunction);
Note: In browsers that support the Web Performance API's high-resolution time feature, Performance.now()
can provide more reliable and precise measurements of elapsed time than Date.now()
.
var seconds = Math.floor(Date.now() / 1000);
In this case it's important to return only an integer (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses Math.floor()
and not Math.round()
).
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'Date' in that specification. | Draft | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Date' in that specification. | Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'Date' in that specification. | Standard | |
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes
|
Yes | Yes |
@@toPrimitive |
? | ? | 44 | ? | ? | ? |
UTC |
Yes | Yes | 1 | Yes | Yes | Yes |
getDate |
Yes | Yes | 1 | Yes | Yes | Yes |
getDay |
Yes | Yes | 1 | Yes | Yes | Yes |
getFullYear |
Yes | Yes | 1 | Yes | Yes | Yes |
getHours |
Yes | Yes | 1 | Yes | Yes | Yes |
getMilliseconds |
Yes | Yes | 1 | Yes | Yes | Yes |
getMinutes |
Yes | Yes | 1 | Yes | Yes | Yes |
getMonth |
Yes | Yes | 1 | Yes | Yes | Yes |
getSeconds |
Yes | Yes | 1 | Yes | Yes | Yes |
getTime |
Yes | Yes | 1 | Yes | Yes | Yes |
getTimezoneOffset |
Yes | Yes | 1 | 5 | Yes | Yes |
getUTCDate |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCDay |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCFullYear |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCHours |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCMilliseconds |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCMinutes |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCMonth |
Yes | Yes | 1 | Yes | Yes | Yes |
getUTCSeconds |
Yes | Yes | 1 | Yes | Yes | Yes |
getYear
|
Yes | Yes | 1 | Yes | Yes | Yes |
now |
5 | Yes | 3 | 9 | 10.5 | 4 |
parse |
Yes | Yes | 1 | Yes | Yes | Yes |
parse : ISO 8601 format |
Yes | ? | 4 | 9 | Yes | Yes |
prototype |
Yes | Yes | 1 | Yes | Yes | Yes |
prototype : Ordinary object (ES2015) |
? | ? | 41 | ? | ? | ? |
setDate |
Yes | Yes | 1 | Yes | Yes | Yes |
setFullYear |
Yes | Yes | 1 | Yes | Yes | Yes |
setHours |
Yes | Yes | 1 | Yes | Yes | Yes |
setMilliseconds |
Yes | Yes | 1 | Yes | Yes | Yes |
setMinutes |
Yes | Yes | 1 | Yes | Yes | Yes |
setMonth |
Yes | Yes | 1 | Yes | Yes | Yes |
setSeconds |
Yes | Yes | 1 | Yes | Yes | Yes |
setTime |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCDate |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCFullYear |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCHours |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCMilliseconds |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCMinutes |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCMonth |
Yes | Yes | 1 | Yes | Yes | Yes |
setUTCSeconds |
Yes | Yes | 1 | Yes | Yes | Yes |
setYear
|
Yes | Yes | 1 | Yes | Yes | Yes |
toDateString |
Yes | Yes | 1 | Yes | Yes | Yes |
toGMTString
|
Yes | Yes | 1 | Yes | Yes | Yes |
toISOString |
Yes | Yes | 1 | 9 | Yes | Yes |
toJSON |
Yes | Yes | 1 | 8 | Yes | Yes |
toLocaleDateString |
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleDateString.locales |
24 | Yes | 29 | 11 | 15 | 10 |
toLocaleDateString.options |
24 | Yes | 29 | 11 | 15 | 10 |
toLocaleDateString : IANA time zone names in timeZone option |
24 | ? | 52 | ? | ? | ? |
toLocaleFormat
|
No | No | 1.5 — 58 | No | No | No |
toLocaleString |
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleString.locales |
24 | ? | 29 | 11 | 15 | 10 |
toLocaleString.options |
24 | ? | 29 | 11 | 15 | 10 |
toLocaleString : IANA time zone names in timeZone option |
24 | ? | 52 | ? | ? | ? |
toLocaleTimeString |
Yes | Yes | 1 | Yes | Yes | Yes |
toLocaleTimeString.locales |
24 | ? | 29 | 11 | 15 | 10 |
toLocaleTimeString.options |
24 | ? | 29 | 11 | 15 | 10 |
toLocaleTimeString : IANA time zone names in timeZone option |
24 | ? | 52 | ? | ? | ? |
toSource
|
No | No | 1 | No | No | No |
toString |
Yes | Yes | 1 | Yes | Yes | Yes |
toTimeString |
Yes | Yes | 1 | Yes | Yes | Yes |
toUTCString |
Yes | Yes | 1 | Yes | Yes | Yes |
valueOf |
Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
@@toPrimitive |
? | ? | ? | 44 | ? | ? | ? |
UTC |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getDate |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getDay |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getFullYear |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getHours |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getMilliseconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getMinutes |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getMonth |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getSeconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getTime |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getTimezoneOffset |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCDate |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCDay |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCFullYear |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCHours |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCMilliseconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCMinutes |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCMonth |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getUTCSeconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getYear
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
now |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
parse |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
parse : ISO 8601 format |
Yes | Yes | ? | 4 | Yes | Yes | Yes |
prototype |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
prototype : Ordinary object (ES2015) |
? | ? | ? | 41 | ? | ? | ? |
setDate |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setFullYear |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setHours |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setMilliseconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setMinutes |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setMonth |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setSeconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setTime |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCDate |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCFullYear |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCHours |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCMilliseconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCMinutes |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCMonth |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setUTCSeconds |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
setYear
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toDateString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toGMTString
|
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toISOString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toJSON |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleDateString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleDateString.locales |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleDateString.options |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleDateString : IANA time zone names in timeZone option |
? | ? | ? | No | ? | ? | ? |
toLocaleFormat
|
No | No | No | 4 — 58 | No | No | No |
toLocaleString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleString.locales |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleString.options |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleString : IANA time zone names in timeZone option |
? | ? | ? | No | ? | ? | ? |
toLocaleTimeString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toLocaleTimeString.locales |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleTimeString.options |
No | 26 | ? | 56 | No | 10 | Yes |
toLocaleTimeString : IANA time zone names in timeZone option |
? | ? | ? | No | ? | ? | ? |
toSource
|
No | No | No | 4 | No | No | No |
toString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toTimeString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
toUTCString |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
valueOf |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
@@toPrimitive |
6.0.0 |
UTC |
Yes |
getDate |
Yes |
getDay |
Yes |
getFullYear |
Yes |
getHours |
Yes |
getMilliseconds |
Yes |
getMinutes |
Yes |
getMonth |
Yes |
getSeconds |
Yes |
getTime |
Yes |
getTimezoneOffset |
Yes |
getUTCDate |
Yes |
getUTCDay |
Yes |
getUTCFullYear |
Yes |
getUTCHours |
Yes |
getUTCMilliseconds |
Yes |
getUTCMinutes |
Yes |
getUTCMonth |
Yes |
getUTCSeconds |
Yes |
getYear
|
Yes |
now |
Yes |
parse |
Yes |
parse : ISO 8601 format |
? |
prototype |
Yes |
prototype : Ordinary object (ES2015) |
? |
setDate |
Yes |
setFullYear |
Yes |
setHours |
Yes |
setMilliseconds |
Yes |
setMinutes |
Yes |
setMonth |
Yes |
setSeconds |
Yes |
setTime |
Yes |
setUTCDate |
Yes |
setUTCFullYear |
Yes |
setUTCHours |
Yes |
setUTCMilliseconds |
Yes |
setUTCMinutes |
Yes |
setUTCMonth |
Yes |
setUTCSeconds |
Yes |
setYear
|
Yes |
toDateString |
Yes |
toGMTString
|
Yes |
toISOString |
Yes |
toJSON |
Yes |
toLocaleDateString |
? |
toLocaleDateString.locales |
? |
toLocaleDateString.options |
? |
toLocaleDateString : IANA time zone names in timeZone option |
? |
toLocaleFormat
|
No |
toLocaleString |
? |
toLocaleString.locales |
? |
toLocaleString.options |
? |
toLocaleString : IANA time zone names in timeZone option |
? |
toLocaleTimeString |
? |
toLocaleTimeString.locales |
? |
toLocaleTimeString.options |
? |
toLocaleTimeString : IANA time zone names in timeZone option |
? |
toSource
|
No |
toString |
Yes |
toTimeString |
Yes |
toUTCString |
Yes |
valueOf |
Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date