Why does Date.parse not return a Date object?

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

Tags:

Javascript