javascript RFC 2822 和 ISO 8601 日期格式正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25362713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RFC 2822 and ISO 8601 date format regex
提问by jwaliszko
In JavaScript there is Date.parse()
method, which parses a string representing an RFC 2822or ISO 8601date (see MDN). Among tons of various sources on the web, what are the most reliable and comprehensive regular expressions able to match those date formats (separately)?
在 JavaScript 中有一个Date.parse()
方法,它解析表示RFC 2822或ISO 8601日期的字符串(参见 MDN)。在网络上的大量各种来源中,能够(单独)匹配这些日期格式的最可靠和最全面的正则表达式是什么?
UPDATE:If there is no reasonable way to get comprehensive regex to match these formats entirely, at least what are the patterns for these particularRFC and ISO formats, Date.parse()
method accepts and understands correctly.
更新:如果没有合理的方法来获得全面的正则表达式来完全匹配这些格式,至少这些特定RFC 和 ISO 格式的模式是什么,Date.parse()
方法可以正确接受和理解。
回答by Rahul Tripathi
I think the answer would be to say that there is no single regex(or rather a bad idea to approach with as it would be very tricky and difficult) that would match all the formats which are listed in the RFC 2822 or ISO 8601. Also it would not be a safe and good approach having a regex for all the formats. However, if you have any specific format then yes we can go for a regex.
我认为答案是说没有一个正则表达式(或者更确切地说是一个坏主意,因为它非常棘手和困难)可以匹配 RFC 2822 或 ISO 8601 中列出的所有格式。另外对所有格式都使用正则表达式并不是一种安全且好的方法。但是,如果您有任何特定格式,那么是的,我们可以使用 regex。
You may check date.jsand moment.js
EDIT:
编辑:
The same MDN says:
同一个 MDN 说:
Parameters
dateString
A string representing an RFC822or ISO 8601date.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.
参数
dateString
表示RFC822或ISO 8601日期的字符串。描述
parse 方法接受一个日期字符串(例如“Dec 25, 1995”)并返回自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。本地时区用于解释不包含时区信息的参数。此函数可用于根据字符串值设置日期值,例如与 setTime 方法和 Date 对象结合使用。
给定一个表示时间的字符串,parse 返回时间值。它接受 RFC822 / IETF 日期语法(RFC 1123 Section 5.2.14 和其他地方),例如 "Mon, 25 Dec 1995 13:30:00 GMT"。它理解美国大陆时区的缩写,但对于一般用途,使用时区偏移,例如,“Mon, 25 Dec 1995 13:30:00 GMT+0430”(格林威治以东 4 小时 30 分钟子午线)。如果未指定时区,则假定为本地时区。GMT 和 UTC 被认为是等效的。
或者,日期/时间字符串可以采用 ISO 8601 格式。从 JavaScript 1.8.5 / Firefox 4 开始,支持 ISO 8601 的一个子集。例如,可以传递和解析“2011-10-10”(仅日期)或“2011-10-10T14:48:00(日期和时间)。尚不支持 ISO 日期中的时区,例如“2011- 10-10T14:48:00+0200"(带时区)还没有给出预期的结果。
From 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”.
此格式包括仅日期格式:
- YYYY
- YYYY-MM
- YYYY-MM-DD
...
所有数字必须以 10 为基数。如果 MM 或 DD 字段不存在,则使用“01”作为值。如果 mm 或 ss 字段不存在,则使用“00”作为值并且不存在的 sss 文件的值为“000”。不存在时区偏移的值是“Z”。
Also check this
也检查这个