The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.
Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.
But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.
To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').
To display a date in UTC, use .toUTCString(),
to display a date in user's local time, use .toString().
More info on MDN | Date and this answer.
For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). Note that the number of the month must be 1 less.
Alternate method - use an appropriate library:
You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.
Answer from Pavel Hodek on Stack OverflowParsing a string to a date in JavaScript - Stack Overflow
[AskJS] Are there any JS libraries that will parse a date string and return a format string?
Parse Date to Specific Cultures (JavaScript)
How to parse datetime string into JavaScript Date object
Videos
The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.
Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.
But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC.
To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').
To display a date in UTC, use .toUTCString(),
to display a date in user's local time, use .toString().
More info on MDN | Date and this answer.
For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00'). Note that the number of the month must be 1 less.
Alternate method - use an appropriate library:
You can also take advantage of the library Moment.js that allows parsing date with the specified time zone.
Unfortunately I found out that
var mydate = new Date('2014-04-03');
console.log(mydate.toDateString());
returns "Wed Apr 02 2014". I know it sounds crazy, but it happens for some users.
The bulletproof solution is the following:
var parts ='2014-04-03'.split('-');
// Please pay attention to the month (parts[1]); JavaScript counts months from 0:
// January - 0, February - 1, etc.
var mydate = new Date(parts[0], parts[1] - 1, parts[2]);
console.log(mydate.toDateString());
For example, if I had the string '1993-03-14 05:45:13' I would want this library to return 'YYYY-MM-DD HH:mm:ss'.
My google searches aren't giving me much, mostly because almost everyone is more interested in just being able to parse the strings, not in getting out what format they were in.
To answer the question in the title: Because they decided so when creating the JavaScript language. Probably because Java's java.util.Date parse function was doing the same thing, and they wanted to mimic its behavior to make the language feel more familiar.
To answer the question in the text... Use this construct to get two date objects:
var today2 = new Date(Date.parse("2008-10-28"));
EDIT: A simple
var today2 = new Date("2008-10-28");
also works.
Note: Old Internet Explorer versions (anything before 9) does not understand dashes in the date string. It works with slashes, though:
var today2 = new Date("2008/10/28");
Slashes seem to be universally understood by browsers both old and new.
If I remember correctly, Date gives you a value down to the millisecond you created the Date object. So unless this code runs exactly on 2008-28-10 at 00:00:00:000, they won't be the same.
Just an addition: Date.parse() by definition returns a long value representing the millisecond value of the Date, and not the Date object itself. If you want to hold the Date object itself, just build it like so:
var newDate = new Date();
newDate.setFullYear(2008,9,28);
For more reference check out: the Date class reference