创建一个新的 Date 对象

要创建新的 Date 对象,请使用 Date() 构造函数:

  • 没有争论

    Date() 创建一个包含当前时间(最长毫秒)和日期的 Date 实例。

  • 有一个整数参数

    Date(m) 创建一个 Date 实例,其中包含与 Epoch 时间(1970 年 1 月 1 日,1970 年 1 月 1 日)加上 m 毫秒相对应的时间和日期。示例:new Date(749019369738) 给出日期 Sun,26 Sep 1993 04:56:09 GMT

  • 带字符串参数

    Date(dateString) 返回 dateString 后使用 Date.parse 解析 dateString 后得到的 Date 对象。

  • 有两个或多个整数参数

    Date(i1, i2, i3, i4, i5, i6) 将参数读取为年,月,日,小时,分钟,秒,毫秒并实例化相应的 Date 对象。请注意,月份在 JavaScript 中为 0 索引,因此 0 表示 1 月,11 表示 12 月。示例:new Date(2017, 5, 1) 于 2017 年 6 月 1 日发布

探索日期

请注意,这些示例是在夏令时期间在美国中部时区的浏览器上生成的,如代码所示。与 UTC 的比较具有指导意义,Date.prototype.toISOString() 用于显示 UTC 的日期和时间(格式化字符串中的 Z 表示 UTC)。

// Creates a Date object with the current date and time from the 
// user's browser
var now = new Date();
now.toString() === 'Mon Apr 11 2016 16:10:41 GMT-0500 (Central Daylight Time)' 
// true
// well, at the time of this writing, anyway

// Creates a Date object at the Unix Epoch (i.e., '1970-01-01T00:00:00.000Z')
var epoch = new Date(0);
epoch.toISOString() === '1970-01-01T00:00:00.000Z' // true

// Creates a Date object with the date and time 2,012 milliseconds 
// after the Unix Epoch (i.e., '1970-01-01T00:00:02.012Z').
var ms = new Date(2012); 
date2012.toISOString() === '1970-01-01T00:00:02.012Z' // true

// Creates a Date object with the first day of February of the year 2012 
// in the local timezone.
var one = new Date(2012, 1);
one.toString() === 'Wed Feb 01 2012 00:00:00 GMT-0600 (Central Standard Time)' 
// true

// Creates a Date object with the first day of the year 2012 in the local 
// timezone.
// (Months are zero-based) 
var zero = new Date(2012, 0);
zero.toString() === 'Sun Jan 01 2012 00:00:00 GMT-0600 (Central Standard Time)' 
// true

// Creates a Date object with the first day of the year 2012, in UTC.
var utc = new Date(Date.UTC(2012, 0));
utc.toString() === 'Sat Dec 31 2011 18:00:00 GMT-0600 (Central Standard Time)'
// true
utc.toISOString() === '2012-01-01T00:00:00.000Z'
// true

// Parses a string into a Date object (ISO 8601 format added in ECMAScript 5.1)
// Implementations should assumed UTC because of ISO 8601 format and Z designation
var iso = new Date('2012-01-01T00:00:00.000Z');
iso.toISOString() === '2012-01-01T00:00:00.000Z' // true

// Parses a string into a Date object (RFC in JavaScript 1.0)
var local = new Date('Sun, 01 Jan 2012 00:00:00 -0600');
local.toString() === 'Sun Jan 01 2012 00:00:00 GMT-0600 (Central Standard Time)'
// true

// Parses a string in no particular format, most of the time. Note that parsing
// logic in these cases is very implementation-dependent, and therefore can vary
// across browsers and versions.
var anything = new Date('11/12/2012'); 
anything.toString() === 'Mon Nov 12 2012 00:00:00 GMT-0600 (Central Standard Time)'
// true, in Chrome 49 64-bit on Windows 10 in the en-US locale. Other versions in 
// other locales may get a different result.

// Rolls values outside of a specified range to the next value.
var rollover = new Date(2012, 12, 32, 25, 62, 62, 1023);
rollover.toString() === 'Sat Feb 02 2013 02:03:03 GMT-0600 (Central Standard Time)'
// true; note that the month rolled over to Feb; first the month rolled over to 
// Jan based on the month 12 (11 being December), then again because of the day 32
// (January having 31 days).

// Special dates for years in the range 0-99
var special1 = new Date(12, 0);
special1.toString() === 'Mon Jan 01 1912 00:00:00 GMT-0600 (Central Standard Time)`
// true

// If you actually wanted to set the year to the year 12 CE, you'd need to use the
// setFullYear() method:
special1.setFullYear(12);
special1.toString() === 'Sun Jan 01   12 00:00:00 GMT-0600 (Central Standard Time)`
// true