undocumented supported Date.parse format?

后端 未结 1 819
不知归路
不知归路 2021-01-07 12:17

MDN\'s documentation for Date.parse says:

Parameters

dateString A string representing an RFC822 or

相关标签:
1条回答
  • 2021-01-07 12:35

    It isn't. The baseline requirements are outlined in the spec but implementations are allowed to support 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.

    Obviously, these can vary widely, and some commonly used formats are ambiguous such as "1/2/1970" which might be the first of February or the second of January. If you want to write cross-platform code, only rely on the baseline formats in 15.9.1.15.

    EDIT:

    Re the difference between YYYY-MM-DD and YYYY/MM/DD, the first is speced as returning that date with a zero time, whereas the second is not specified so an implementation is allowed to produce any date it likes. The relevant portion of the sepc is

    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”.

    0 讨论(0)
提交回复
热议问题