Date.parse()

Date.parse() 静态方法解析日期的字符串表示形式,并返回日期的 timestamp

¥The Date.parse() static method parses a string representation of a date, and returns the date's timestamp.

仅明确指定支持 日期时间字符串格式。其他格式是实现定义的,可能不适用于所有浏览器。如果要容纳多种不同的格式,库可以提供帮助。

¥Only the date time string format is explicitly specified to be supported. Other formats are implementation-defined and may not work across all browsers. A library can help if many different formats are to be accommodated.

Try it

语法

¥Syntax

js
Date.parse(dateString)

参数

¥Parameters

dateString

: 日期时间字符串格式 中的字符串。有关使用不同格式的注意事项,请参阅链接的参考。

返回值

¥Return value

代表给定日期的 timestamp 的数字。如果 dateString 未能解析为有效日期,则返回 NaN

¥A number representing the timestamp of the given date. If dateString fails to be parsed as a valid date, NaN is returned.

描述

¥Description

此函数对于根据字符串值设置日期值非常有用,例如与 setTime() 方法结合使用。

¥This function is useful for setting date values based on string values, for example in conjunction with the setTime() method.

因为 parse()Date 的静态方法,所以你始终将其用作 Date.parse(),而不是用作你创建的 Date 对象的方法。

¥Because parse() is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.

示例

¥Examples

使用 Date.parse()

¥Using Date.parse()

以下调用均返回 1546300800000。第一个将暗示 UTC 时间,因为它仅包含日期,而其他则明确指定 UTC 时区。

¥The following calls all return 1546300800000. The first will imply UTC time because it's date-only, and the others explicitly specify the UTC timezone.

js
Date.parse("2019-01-01");
Date.parse("2019-01-01T00:00:00.000Z");
Date.parse("2019-01-01T00:00:00.000+00:00");

以下未指定时区的调用将被设置为系统本地时区中的 2019-01-01 的 00:00:00,因为它同时具有日期和时间。

¥The following call, which does not specify a time zone will be set to 2019-01-01 at 00:00:00 in the local timezone of the system, because it has both date and time.

js
Date.parse("2019-01-01T00:00:00");

非标准日期字符串

¥Non-standard date strings

注意:本节包含特定于实现的行为,这些行为在实现之间可能不一致。

¥Note: This section contains implementation-specific behavior that can be inconsistent across implementations.

当日期字符串非标准时,实现通常默认为本地时区。为了保持一致性,我们假设代码使用 UTC 时区。

¥Implementations usually default to the local time zone when the date string is non-standard. For consistency, we will assume that the code uses the UTC timezone.

注意:本地时区偏移来自设备的系统设置,然后应用于正在解析的日期。当地时区的夏令时 (DST) 也会对此产生影响

¥Note: The local time zone offset comes from the system setting of the device and is then applied to the date being parsed. Daylight Saving Time (DST), of the local time zone, can also have an effect on this too.

js
Date.parse("Jan 1, 1970"); // 0 in all implementations

Date.parse("Thu, 01 Jan 1970 00:00:00"); // 0 in all implementations

Date.parse("1970,1,1"); // 0 in Chrome and Firefox, NaN in Safari

Date.parse("02 01 1970");
// 2678400000 in Chrome and Firefox (Sun Feb 01 1970 00:00:00 GMT+0000);
// NaN in Safari

// With explicit timezone
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT+0300");
// -10800000 in all implementations in all timezones

// Single number
Date.parse("0");
// NaN in Firefox ≤122
// 946684800000 in Chrome and Firefox ≥123  (Sat Jan 01 2000 00:00:00 GMT+0000);
// -62167219200000 in Safari (Sat Jan 01 0000 00:00:00 GMT+0000)

// Two-digit number that may be a month
Date.parse("28");
// NaN Chrome and Firefox
// -61283606400000 in Safari (Fri Dec 31 0027 23:58:45 GMT-0001)

// Two-digit year
Date.parse("70/01/01"); // 0 in all implementations

// Out-of-bounds date components
Date.parse("2014-25-23"); // NaN in all implementations
Date.parse("Mar 32, 2014"); // NaN in all implementations
Date.parse("2014/25/23"); // NaN in all implementations

Date.parse("2014-02-30");
// NaN in Safari
// 1393718400000 in Chrome and Firefox (Sun Mar 02 2014 00:00:00 GMT+0000)
Date.parse("02/30/2014"); // 1393718400000 in all implementations

// Chrome, Safari, and Firefox 122 and later parse only the first three letters for the month.
// FF121 and earlier parse first three letters and any substring up to the correct month name.
Date.parse("04 Dec 1995"); // 818031600000 in all implementations
Date.parse("04 Decem 1995"); // 818031600000 in all implementations
Date.parse("04 December 1995"); // 818031600000 in all implementations
Date.parse("04 DecFoo 1995"); // NaN in Firefox 121 and earlier. 818031600000 in other implementations
Date.parse("04 De 1995"); // NaN in all implementations

规范

Specification
ECMAScript Language Specification
# sec-date.parse

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also