views:

36

answers:

1
alert(Date.parse('Mar 1 1990'));

in jsFiddle, this returns a datetime integer, as expected. On my machine, it returns... a timestamp string?

Thu Mar 01 1990 00:00:00 GMT-0500 (EST) vs 636267600000

+2  A: 

ECMAScript Language Specification

15.9.4.2 Date.parse (string)

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

What you probably did wrong was that the two tests were not the same:

alert(Date.parse('Mar 1 1990')); returns a number (always)

alert(new Date('Mar 1 1990')); returns the string you mentioned

(because toString method is called on the date object)

galambalazs
@galambalazs +1 I would tend to agree in that the two tests must differ in some way! However `alert(Date('Mar 1 1990'));` will actually output the _current date_, not the string mentioned. When `Date()` is called as a function then any arguments are ignored and a string representation of the current date/time is returned. However, `alert(new Date('Mar 1 1990'));` _will_ return the string mentioned (depending on your locality).
w3d
@w3d yep, this is what i wanted to write. It's a bit late here. Thanks for pointing out.
galambalazs