Which date formats are IETF-compliant RFC 2822 timestamps?

38,200

Solution 1

MSDN has several examples of valid date formats:

document.writeln((new Date("2010")).toUTCString()); 

document.writeln((new Date("2010-06")).toUTCString());

document.writeln((new Date("2010-06-09")).toUTCString());

 // Specifies Z, which indicates UTC time.
document.writeln((new Date("2010-06-09T15:20:00Z")).toUTCString());

 // Specifies -07:00 offset, which is equivalent to Pacific Daylight time.
document.writeln((new Date("2010-06-09T15:20:00-07:00")).toGMTString());

// Specifies a non-ISO Long date.
document.writeln((new Date("June 9, 2010")).toUTCString());

// Specifies a non-ISO Long date.
document.writeln((new Date("2010 June 9")).toUTCString());

// Specifies a non-ISO Short date and time.
document.writeln((new Date("6/9/2010 3:20 pm")).toUTCString());

// Output:
// Fri, 1 Jan 2010 00:00:00 UTC
// Tue, 1 Jun 2010 00:00:00 UTC
// Wed, 9 Jun 2010 00:00:00 UTC
// Wed, 9 Jun 2010 15:20:00 UTC
// Wed, 9 Jun 2010 22:20:00 UTC
// Wed, 9 Jun 2010 07:00:00 UTC
// Wed, 9 Jun 2010 07:00:00 UTC
// Wed, 9 Jun 2010 22:20:00 UTC

Gotchas

There's a matrix of cross-browser inconsistencies as well.

References

Solution 2

This question seems to be asking "What formats are required to be parsed by ECMAScript implementations".

Prior to ECMAScript Ed 5 (2011), parsing was entirely implementation dependent. The formats that ECMAScript implementations are required to parse can be summarised as:

  1. A single (though slightly modified) version of ISO 8601 extended format called the "date time string format" introduced in ECMAScript ed 5 (2011). It differs from ISO 8601 in that date-only forms are treated as UTC, not local and the timezone offset must have a colon between the hours and minutes.
  2. The format produced by an implementation's own toString method, which was standardised in ECMAScript 2018
  3. The format produced by an implementation's own toUTCString method, also standardised in ECMAScript 2018

Parsing of any other format remains implementation dependent and there are differences, so the general rule is "don't use the built-in parser".

Share:
38,200
Oriol
Author by

Oriol

What happened with Time to take a stand made me stop contributing to the Stack Overflow community. I explained my feelings here. Eventually the issue was solved correctly and the question was locked with the proper message. I don't hold a grudge, but at the moment I have other projects at hands, so I don't plan to become actively involved again. And thanks for the 100k cup, I don't like coffee but since it's so big I use it to make crêpes :) § Quotes I like: Java is to JavaScript what Car is to Carpet The argument of speed in JS engines is only dead in the mind of people who can't function without jQuery, since it's an argument they can't win. — by Blue Skies, posted here. § Funny: jQuery is taking over the world! Zalgo is coming!

Updated on July 09, 2022

Comments

  • Oriol
    Oriol almost 2 years

    I need to parse dates in JavaScript. The format is

    [2 digits day]/[2 digits month]/[4 digits year] [2 digits hour (24 mode)]:[2 digits minute]

    For example, 16/02/2013 21:00

    But if I do new Date('16/02/2013 21:00').toString(), it gives 'Wed Apr 02 2014 21:00:00 GMT+0200 (Hora de verano romance)'.

    I guess that's because my dates don't follow IETF RFC 2822 Date and Time Specification. Then, I should convert my string, and I want to convert it to the most similar compliant format (because it should be easier to convert). But https://www.rfc-editor.org/rfc/rfc2822#page-14 is hard to understand, so I don't know which is the most similar format.

    Is there a list with examples of the allowed formats?