gpt4 book ai didi

javascript - RFC 2822 和 ISO 8601 日期格式正则表达式

转载 作者:行者123 更新时间:2023-11-29 19:35:20 26 4
gpt4 key购买 nike

在 JavaScript 中有 Date.parse() 方法,它解析表示 RFC 2822 的字符串。或 ISO 8601日期(see MDN)。在网络上的大量各种来源中,能够匹配这些日期格式(单独)的最可靠和最全面的正则表达式是什么?

更新:如果没有合理的方法来获得全面的正则表达式来完全匹配这些格式,至少这些特定 RFC 和 ISO 格式的模式是什么,Date.parse() 方法正确接受和理解。

最佳答案

我认为答案是没有一个正则表达式(或者更确切地说是一个坏主意,因为它会非常棘手和困难)将匹配 RFC 2822 或 ISO 8601 中列出的所有格式。此外,对所有格式使用正则表达式也不是一种安全且好的方法。但是,如果您有任何 specific format then yes we can go for a regex .

您可以查看date.jsmoment.js

编辑:

同样的 MDN 说:

Parameters

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

来自 here

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

同时检查 this

关于javascript - RFC 2822 和 ISO 8601 日期格式正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362713/

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