Date() constructor
Creates a JavaScript Date instance that represents a single moment in time in a platform-independent format.Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
Syntax
new Date() new Date(value) new Date(dateString) new Date(dateObject) new Date(year, monthIndex) new Date(year, monthIndex, day) new Date(year, monthIndex, day, hours) new Date(year, monthIndex, day, hours, minutes) new Date(year, monthIndex, day, hours, minutes, seconds) new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
Note: The only correct way to instantiate a new Date object is by using the new operator. If you call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.
Parameters
There are five basic forms for the Date() constructor:
- No parameters When no parameters are provided, the newly-created
Dateobject represents the current date and time as of the time of instantiation. - Time value or timestamp number
value-
An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (the ECMAScript epoch, equivalent to the UNIX epoch), with leap seconds ignored. Keep in mind that most UNIX Timestamp functions are only accurate to the nearest second.
- Timestamp string
dateString-
A string value representing a date, specified in a format recognized by the
Date.parse()method. (These formats are IETF-compliant RFC 2822 timestamps, and also strings in a version of ISO8601.)Note: Parsing of date strings with the
Dateconstructor (andDate.parse(), which works the same way) 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.
- Date object
dateObject-
An existing
Dateobject. This effectively makes a copy of the existingDateobject with the same date and time. This is equivalent to using thenew Date(value)constuctor, wherevalueis the can be obtained using thevalueOf()method.
- Individual date and time component values Given at least a year and month, this form of
Date()returns aDateobject whose component values (year, month, day, hour, minute, second, and millisecond) all come from the following parameters. Any missing fields are given the lowest possible value (1fordayand0for every other component). The parameter values are all evaluated against the local time zone, rather than UTC.year-
Integer value representing the year.
Values from
0to99map to the years1900to1999. All other values are the actual year. See the example. monthIndex-
Integer value representing the month, beginning with
0for January to11for December. If a value greater than11is passed in, then those months will be added to the date; for example,new Date(1990, 12, 1)will return January 1st, 1991 -
dayOptional -
Integer value representing the day of the month. The default is
1. -
hoursOptional -
Integer value between
0and23representing the hour of the day. Defaults to0. -
minutesOptional -
Integer value representing the minute segment of a time. The default is
0minutes past the hour. -
secondsOptional -
Integer value representing the second segment of a time. The default is
0seconds past the minute. -
millisecondsOptional -
Integer value representing the millisecond segment of a time. The default is
0milliseconds past the second.
Return value
Calling new Date() (the Date()constructor) returns a Date object. If called with an invalid date string, it returns a Date object whose toString() method returns the literal string Invalid Date.
Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().toString() does. Any arguments given in a Date() function call (without the new keyword) are ignored; regardless of whether it’s called with an invalid date string — or even called wth any arbitrary object or other primitive as an argument — it always returns a string representation of the current date and time.
Examples
Several ways to create a Date object
The 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.
let today = new Date() let sameDay = new Date(today) let birthday = new Date('December 17, 1995 13:24:00') let birthday = new Date('1995-12-17T13:24:00') let birthday = new Date(1995, 11, 17) // the month is 0-indexed let birthday = new Date(1995, 11, 17, 13, 24, 0)
Specifications
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Date |
1 |
12 |
1 |
3 |
3 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN 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/Date