0

I have an RFC1123 Date string, such as: 'Mon, 01 Jan 0001 00:00:01 GMT'

I want to parse this string into a Date in NodeJS. I assumed that new Date('Mon, 01 Jan 0001 00:00:01 GMT') would achieve what I wanted... but it doesn't. When I inspect the content of the resultant Date I see that .toUTCString() gives Mon, 01 Jan 2001 00:00:01 GMT.

I am at a loss as to how the parsing function determined that the year was 2001.

What's even more bizarre is that if I use ISO8601 format instead, and do: new Date('0001-01-01T00:00:01Z').toUTCString() I get Mon, 01 Jan 1 00:00:01 GMT -- exactly what I would expect -- yet if I then do new Date('Mon, 01 Jan 1 00:00:01 GMT') I still get the year as 2001.

Note: I can process these same strings in another language (C#) and I get exactly what I would expect.

Anybody have any suggestions for how to correctly deserialize RFC1123 Date's in NodeJS? If the date is "new" (i.e. above the year 1000) things seem to work okay, but "old" dates seem broken, or else I'm missing something "extra" which is required for older dates.

edit: It looks like technically the RFC 1123/2822 format has a lower bound of the year 1900 -- although Date allows us to go below that so I am not sure what's stopping it from supporting years <100.

Hydraxy
  • 279
  • 2
  • 9
  • It looks like this is at least how V8 does it:https://github.com/v8/v8/blob/8ad1778b500341e662144a952ec6aa4fa52ddf4c/src/dateparser.cc#L52 Of course, what that means for solving my problem I am not sure... – Hydraxy Oct 13 '15 at 00:55

1 Answers1

3

You could use moment.js to parse the date:

var dateFormat = 'ddd, DD MMM YYYY HH:mm:ss',
  dateString = 'Mon, 01 Jan 0001 00:00:01 GMT';

var dateTime = moment.utc(dateString, dateFormat);
Evan Siroky
  • 9,040
  • 6
  • 54
  • 73