Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Date
  6. Date()

Date() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheDate() constructor createsDate objects. When called as a function, it returns a string representing the current time.

Try it

const date1 = new Date("December 17, 1995 03:24:00");// Sun Dec 17 1995 03:24:00 GMT...const date2 = new Date("1995-12-17T03:24:00");// Sun Dec 17 1995 03:24:00 GMT...console.log(date1.getTime() === date2.getTime());// Expected output: true

Syntax

js
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)Date()

Note:Date() can be called with or withoutnew, but with different effects. SeeReturn value.

Parameters

There are five basic forms for theDate() constructor:

No parameters

When no parameters are provided, the newly-createdDate object represents the current date and time as of the time of instantiation. The returned date'stimestamp is the same as the number returned byDate.now().

Time value or timestamp number

value

An integer value representing thetimestamp (the number of milliseconds since midnight at the beginning of January 1, 1970, UTC — a.k.a. theepoch).

Date string

dateString

A string value representing a date, parsed and interpreted using the same algorithm implemented byDate.parse(). Seedate time string format for caveats on using different formats.

Date object

dateObject

An existingDate object. This effectively makes a copy of the existingDate object with the same date and time. This is equivalent tonew Date(dateObject.valueOf()), except thevalueOf() method is not called.

When one parameter is passed to theDate() constructor,Date instances are specially treated. All other values areconverted to primitives. If the result is a string, it will be parsed as a date string. Otherwise, the resulting primitive is further coerced to a number and treated as a timestamp.

Individual date and time component values

Given at least a year and month, this form ofDate() returns aDate object 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 (1 forday and0 for every other component). The parameter values are all evaluated against the local time zone, rather than UTC.Date.UTC() accepts similar parameters but interprets the components as UTC and returns a timestamp.

If any parameter overflows its defined bounds, it "carries over". For example, if amonthIndex greater than11 is passed in, those months will cause the year to increment; if aminutes greater than59 is passed in,hours will increment accordingly, etc. Therefore,new Date(1990, 12, 1) will return January 1st, 1991;new Date(2020, 5, 19, 25, 65) will return 2:05 A.M. June 20th, 2020.

Similarly, if any parameter underflows, it "borrows" from the higher positions. For example,new Date(2020, 5, 0) will return May 31st, 2020.

year

Integer value representing the year. Values from0 to99 map to the years1900 to1999. All other values are the actual year. See theexample.

monthIndex

Integer value representing the month, beginning with0 for January to11 for December.

dayOptional

Integer value representing the day of the month. Defaults to1.

hoursOptional

Integer value between0 and23 representing the hour of the day. Defaults to0.

minutesOptional

Integer value representing the minute segment of a time. Defaults to0.

secondsOptional

Integer value representing the second segment of a time. Defaults to0.

millisecondsOptional

Integer value representing the millisecond segment of a time. Defaults to0.

Return value

Callingnew Date() (theDate() constructor) returns aDate object. If called with an invalid date string, or if the date to be constructed will have a timestamp less than-8,640,000,000,000,000 or greater than8,640,000,000,000,000 milliseconds, it returns aninvalid date (aDate object whosetoString() method returns"Invalid Date" andvalueOf() method returnsNaN).

Calling theDate() function (without thenew keyword) returns a string representation of the current date and time, exactly asnew Date().toString() does. Any arguments given in aDate() function call (without thenew keyword) are ignored; regardless of whether it's called with an invalid date string — or even called with any arbitrary object or other primitive as an argument — it always returns a string representation of the current date and time.

Description

Reduced time precision

To offer protection against timing attacks andfingerprinting, the precision ofnew Date() might get rounded depending on browser settings. In Firefox, theprivacy.reduceTimerPrecision preference is enabled by default and defaults to 2ms. You can also enableprivacy.resistFingerprinting, in which case the precision will be 100ms or the value ofprivacy.resistFingerprinting.reduceTimerPrecision.microseconds, whichever is larger.

For example, with reduced time precision, the result ofnew Date().getTime() will always be a multiple of 2, or a multiple of 100 (orprivacy.resistFingerprinting.reduceTimerPrecision.microseconds) withprivacy.resistFingerprinting enabled.

js
// reduced time precision (2ms) in Firefox 60new Date().getTime();// Might be:// 1519211809934// 1519211810362// 1519211811670// …// reduced time precision with `privacy.resistFingerprinting` enablednew Date().getTime();// Might be:// 1519129853500// 1519129858900// 1519129864400// …

Examples

Several ways to create a Date object

The following examples show several ways to create JavaScript dates:

js
const today = new Date();const birthday = new Date("December 17, 1995 03:24:00"); // DISCOURAGED: may not work in all runtimesconst birthday = new Date("1995-12-17T03:24:00"); // This is standardized and will work reliablyconst birthday = new Date(1995, 11, 17); // the month is 0-indexedconst birthday = new Date(1995, 11, 17, 3, 24, 0);const birthday = new Date(628021800000); // passing epoch timestamp

Passing a non-Date, non-string, non-number value

If theDate() constructor is called with one parameter which is not aDate instance, it will be coerced to a primitive and then checked whether it's a string. For example,new Date(undefined) is different fromnew Date():

js
console.log(new Date(undefined)); // Invalid Date

This is becauseundefined is already a primitive but not a string, so it will be coerced to a number, which isNaN and therefore not a valid timestamp. On the other hand,null will be coerced to0.

js
console.log(new Date(null)); // 1970-01-01T00:00:00.000Z

Arrays would be coerced to a string viaArray.prototype.toString(), which joins the elements with commas. However, the resulting string for any array with more than one element is not a valid ISO 8601 date string, so its parsing behavior would be implementation-defined.Do not pass arrays to theDate() constructor.

js
console.log(new Date(["2020-06-19", "17:13"]));// 2020-06-19T17:13:00.000Z in Chrome, since it recognizes "2020-06-19,17:13"// "Invalid Date" in Firefox

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-date-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp