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.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
new Date() new Date(value) new Date(dateString) new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
注意:
The only correct way to instantiate a new
Date
object is by using the
new
operator. If you simply call the
Date
object directly, such as
now = Date()
, the returned value is a string rather than a
Date
对象。
There are four basic forms for the
Date()
构造函数:
When no parameters are provided, the newly-created
Date
object represents the current date and time as of the time of instantiation.
value
dateString
Date.parse()
method. (These formats are
IETF-compliant RFC 2822 timestamps
, and also strings in a
version of ISO8601
.)
注意:
Parsing of date strings with the
Date
constructor (and
Date.parse()
, which works the same way) is
strongly discouraged
due to browser differences and inconsistencies.
"1970-01-01"
) are treated as UTC, not local.
Given at least a year and month, this form of
Date()
返回
Date
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
for
day
and
0
for every other component).
year
Integer value representing the year.
Values from
0
to
99
map to the years
1900
to
1999
. All other values are the actual year. See the
example below
.
monthIndex
0
for January to
11
for December.
day
可选
1
.
hours
可选
0
(midnight).
minutes
可选
0
minutes past the hour.
seconds
可选
0
seconds past the minute.
毫秒
可选
0
milliseconds past the second.
The following examples show several ways to create JavaScript dates:
注意:
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 birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Date' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Date()
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Date
Date.UTC()
Date.now()
Date.parse()
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()
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.prototype.setUTCDate()
Date.prototype.setUTCFullYear()
Date.prototype.setUTCHours()
Date.prototype.setUTCMilliseconds()
Date.prototype.setUTCMinutes()
Date.prototype.setUTCMonth()
Date.prototype.setUTCSeconds()
Date.prototype.setYear()
Date.prototype.toDateString()
Date.prototype.toGMTString()
Date.prototype.toISOString()
Date.prototype.toJSON()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleString()
Date.prototype.toLocaleTimeString()
Date.prototype.toSource()
Date.prototype.toString()
Date.prototype.toTimeString()
Date.prototype.toUTCString()
Date.prototype.valueOf()
Date.prototype[@@toPrimitive]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()