gpt4 book ai didi

javascript - 未记录支持的 Date.parse 格式?

转载 作者:行者123 更新时间:2023-11-30 18:33:08 25 4
gpt4 key购买 nike

MDN 的 documentation for Date.parse说:

Parameters

dateString A string representing an RFC822 or ISO 8601 date.

Description

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The local time zone is used to interpret arguments that do not contain time zone information. This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.

Given a string representing a time, parse returns the time value. It accepts the RFC822 / IETF date syntax (RFC 1123 Section 5.2.14 and elsewhere), e.g. "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed. Timezones in ISO dates are not yet supported, so e.g. "2011-10-10T14:48:00+0200" (with timezone) does not give the intended result yet.

但是:

var t = new Date("4/25/2010");
console.log(t);

// Output: Sun Apr 25 2010 00:00:00 GMT+0100 (GMT Daylight Time)

它应该写在哪里support MM/dd/yyyy 格式?

最佳答案

事实并非如此。 baseline requirements在规范中进行了概述,但允许实现支持 additional formats of their choosing .

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

显然,这些格式可能有很大差异,并且一些常用格式是模棱两可的,例如“1/2/1970”可能是二月一日或一月二日。如果你想写跨平台的代码,只能依赖 15.9.1.15 中的基线格式。

编辑:

关于 YYYY-MM-DD 和 YYYY/MM/DD 之间的区别,第一个被指定为返回零时间的日期,而第二个没有指定,因此允许实现产生它喜欢的任何日期。 sepc 的相关部分是

This format includes date-only forms:

  • YYYY
  • YYYY-MM
  • YYYY-MM-DD

...

All numbers must be base 10. If the MM or DD fields are absent “01” is used as the value. If the mm or ss fields are absent “00” is used as the value and the value of an absent sss file is “000”. The value of an absent time zone offset is “Z”.

关于javascript - 未记录支持的 Date.parse 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com