Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RFC 2822 and ISO 8601 date format regex

In JavaScript there is Date.parse() method, which parses a string representing an RFC 2822 or ISO 8601 date (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)?

UPDATE: If there is no reasonable way to get comprehensive regex to match these formats entirely, at least what are the patterns for these particular RFC and ISO formats, Date.parse() method accepts and understands correctly.

like image 820
jwaliszko Avatar asked Aug 18 '14 11:08

jwaliszko


1 Answers

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.

You may check date.js and moment.js

EDIT:

The same MDN says:

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.

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

Also check this

like image 131
Rahul Tripathi Avatar answered Nov 17 '22 21:11

Rahul Tripathi