1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
---
title: Date() constructor
slug: Web/JavaScript/Reference/Global_Objects/Date/Date
translation_of: Web/JavaScript/Reference/Global_Objects/Date/Date
---
<div>{{JSRef}}</div>
<p>Creates a JavaScript <strong><code>Date</code></strong> instance that represents a single moment in time in a platform-independent format. <code>Date</code> objects contain a <code>Number</code> that represents milliseconds since 1 January 1970 UTC.</p>
<div>{{EmbedInteractiveExample("pages/js/date-constructor.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">new Date()
new Date(<var>value</var>)
new Date(<var>dateString</var>)
new Date(<var>year</var>, <var>monthIndex</var> [, <var>day</var> [, <var>hours</var> [, <var>minutes</var> [, <var>seconds</var> [, <var>milliseconds</var>]]]]])
</pre>
<div class="note">
<p><strong>备注:</strong> The only correct way to instantiate a new <code>Date</code> object is by using the {{jsxref("new")}} operator. If you simply call the <code>Date</code> object directly, such as <code>now = Date()</code>, the returned value is a string rather than a <code>Date</code> object.</p>
</div>
<h3 id="Parameters">Parameters</h3>
<p>There are four basic forms for the <code>Date()</code> constructor:</p>
<ol>
<li>
<h4 id="No_parameters">No parameters</h4>
<p>When no parameters are provided, the newly-created <code>Date</code> object represents the current date and time as of the time of instantiation.</p>
</li>
<li>
<h4 id="Time_value_or_timestamp_number">Time value or timestamp number</h4>
<dl>
<dt><code><var>value</var></code></dt>
<dd>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 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16">UNIX Timestamp</a> functions are only accurate to the nearest second.</dd>
</dl>
</li>
<li>
<h4 id="Timestamp_string">Timestamp string</h4>
<dl>
<dt><code><var>dateString</var></code></dt>
<dd>A string value representing a date, specified in a format recognized by the {{jsxref("Date.parse()")}} method. (These formats are <a href="http://tools.ietf.org/html/rfc2822#page-14">IETF-compliant RFC 2822 timestamps</a>, and also strings in a <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15">version of ISO8601</a>.)
<div class="note">
<p><strong>Note:</strong> Parsing of date strings with the <code>Date</code> constructor (and <code>Date.parse()</code>, which works the same way) is <em>strongly discouraged</em> due to browser differences and inconsistencies.</p>
<ul>
<li>Support for <a href="https://tools.ietf.org/html/rfc2822">RFC 2822</a> format strings is by convention only.</li>
<li>Support for ISO 8601 formats differs in that date-only strings (e.g. <code>"1970-01-01"</code>) are treated as UTC, not local.</li>
</ul>
</div>
</dd>
</dl>
</li>
<li>
<h4 id="Individual_date_and_time_component_values">Individual date and time component values</h4>
<p>Given at least a year and month, this form of <code>Date()</code> returns a <code>Date</code> 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 (<code>1</code> for <code><var>day</var></code> and <code>0</code> for every other component).</p>
<dl>
<dt><code><var>year</var></code></dt>
<dd>
<p>Integer value representing the year.</p>
<p>Values from <code>0</code> to <code>99</code> map to the years <code>1900</code> to <code>1999</code>. All other values are the actual year. See the <a href="#Two_digit_years_map_to_1900_-_1999">example below</a>.</p>
</dd>
<dt><code><var>monthIndex</var></code></dt>
<dd>Integer value representing the month, beginning with <code>0</code> for January to <code>11</code> for December.</dd>
<dt><code><var>day</var></code> {{optional_inline}}</dt>
<dd>Integer value representing the day of the month. The default is <code>1</code>.</dd>
<dt><code><var>hours</var></code> {{optional_inline}}</dt>
<dd>Integer value representing the hour of the day. The default is <code>0</code> (midnight).</dd>
<dt><code><var>minutes</var></code> {{optional_inline}}</dt>
<dd>Integer value representing the minute segment of a time. The default is <code>0</code> minutes past the hour.</dd>
<dt><code><var>seconds</var></code> {{optional_inline}}</dt>
<dd>Integer value representing the second segment of a time. The default is <code>0</code> seconds past the minute.</dd>
<dt><code><var>milliseconds</var></code> {{optional_inline}}</dt>
<dd>Integer value representing the millisecond segment of a time. The default is <code>0</code> milliseconds past the second.</dd>
</dl>
</li>
</ol>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-date-constructor', 'Date')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.builtins.Date.Date")}}</p>
|