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() 构造函数:

  1. No parameters

    When no parameters are provided, the newly-created Date object represents the current date and time as of the time of instantiation.

  2. 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.
  3. 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 .)

    注意: 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.

    • 支持 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.
  4. Individual date and time component values

    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
    Integer value representing the month, beginning with 0 for January to 11 for December.
    day 可选
    Integer value representing the day of the month. The default is 1 .
    hours 可选
    Integer value representing the hour of the day. The default is 0 (midnight).
    minutes 可选
    Integer value representing the minute segment of a time. The default is 0 minutes past the hour.
    seconds 可选
    Integer value representing the second segment of a time. The default is 0 seconds past the minute.
    毫秒 可选
    Integer value representing the millisecond segment of a time. The default is 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.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Date
  3. 方法
    1. Date.UTC()
    2. Date.now()
    3. Date.parse()
    4. Date.prototype.getDate()
    5. Date.prototype.getDay()
    6. Date.prototype.getFullYear()
    7. Date.prototype.getHours()
    8. Date.prototype.getMilliseconds()
    9. Date.prototype.getMinutes()
    10. Date.prototype.getMonth()
    11. Date.prototype.getSeconds()
    12. Date.prototype.getTime()
    13. Date.prototype.getTimezoneOffset()
    14. Date.prototype.getUTCDate()
    15. Date.prototype.getUTCDay()
    16. Date.prototype.getUTCFullYear()
    17. Date.prototype.getUTCHours()
    18. Date.prototype.getUTCMilliseconds()
    19. Date.prototype.getUTCMinutes()
    20. Date.prototype.getUTCMonth()
    21. Date.prototype.getUTCSeconds()
    22. Date.prototype.getYear()
    23. Date.prototype.setDate()
    24. Date.prototype.setFullYear()
    25. Date.prototype.setHours()
    26. Date.prototype.setMilliseconds()
    27. Date.prototype.setMinutes()
    28. Date.prototype.setMonth()
    29. Date.prototype.setSeconds()
    30. Date.prototype.setTime()
    31. Date.prototype.setUTCDate()
    32. Date.prototype.setUTCFullYear()
    33. Date.prototype.setUTCHours()
    34. Date.prototype.setUTCMilliseconds()
    35. Date.prototype.setUTCMinutes()
    36. Date.prototype.setUTCMonth()
    37. Date.prototype.setUTCSeconds()
    38. Date.prototype.setYear()
    39. Date.prototype.toDateString()
    40. Date.prototype.toGMTString()
    41. Date.prototype.toISOString()
    42. Date.prototype.toJSON()
    43. Date.prototype.toLocaleDateString()
    44. Date.prototype.toLocaleString()
    45. Date.prototype.toLocaleTimeString()
    46. Date.prototype.toSource()
    47. Date.prototype.toString()
    48. Date.prototype.toTimeString()
    49. Date.prototype.toUTCString()
    50. Date.prototype.valueOf()
    51. Date.prototype[@@toPrimitive]
  4. 继承:
  5. Function
  6. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  7. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  8. Object
  9. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  10. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()