0

When I use Javascript Date.parse for a date in August, I always get the first. This only happens for the month of August.

alert(Date.parse("20 Aug 2014"));  // give me Sat Sep 20 2014 00:00:00 GMT-0500 (Central Standard Time)
alert(Date.parse("20 Aug 2014"));  // gives me Fri Aug 01 2014 00:00:00 GMT-0500 (Central Standard Time)

What is the reason for this?

Barmar
  • 741,623
  • 53
  • 500
  • 612
ruchou
  • 1
  • `new Date(Date.parse("20 Aug 2014"))` returns `Wed Aug 20 2014 00:00:00 GMT-0400 (EDT)` for me in Chrome. – Barmar Jul 03 '14 at 06:48
  • `Date.parse("20 Aug 2014")` returns a timestamp number, not a date. – Barmar Jul 03 '14 at 06:49
  • `console.log(Date.parse("20 Aug 2014"));` returns `1408510800000` and `console.log(Date.parse("20 Aug 2014"));` right after returns `1408510800000`. I think the comments in your code are incorrect. –  Jul 03 '14 at 06:52
  • my suggestion is use `moment.js` – Kokizzu Jul 03 '14 at 06:55

1 Answers1

1

Date.parse is not documented as accepting a string in that format, so you cannot rely on it doing so.

Date.parse (and the version of new Date that accepts a string) are only required to support these formats of strings:

In my experience, all implementations also support the American mm/dd/yyyy format (even in non-US locales), but that is not specified or documented behavior.

If your input string is in that format, you'll have to parse it yourself or use a library like MomentJS to do it for you.


Side note: In your code, you said that Date.parse was giving you a Date (or a date string; you said the alert was showing "Sat Sep 20 2014 00:00:00 GMT-0500 (Central Standard Time)" and the like). That would be fairly surprising. Date.parse returns a number, not a Date. To get the equivalent Date object, use new Date.


* About "(and slightly incorrect)": In ISO-8601, a string with no timezone indicator on it defines a time in local time. ES5, however, says those are GMT.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875