tags:

views:

1296

answers:

5
today1 = new Date();
today2 = Date.parse("2008-28-10");

To compare the time (millisecond) values of these I have to do the following, because today2 is just a number.

if (today1.getTime() == today2)

Why is this?

+4  A: 

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

Yuval A
That doesn't answer the original question, which - rephrased - was 'Why is the type, returned from Date.parse, an integer, rather than a Date?'. I don't know the answer to this either, but decline the opportunity to offer an incorrect answer as an alternative
belugabob
Like I added, Date.parse by def returns the millisecond value, see the reference I linked to...
Yuval A
A: 

And what is the problem in that? Why you need to to do it in one step? Is there any business need?

mnour
My problem is that I, and I'm sure others, would expect a parse method on an object to return an object of the type the method is called on.
ProfK
+8  A: 

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 that Internet Explorer (i.e. JScript) does not understand dashes in the date string. It works with slashes, though:

var today2 = new Date("2008/10/28");
Tomalak
+1  A: 

What Data.parse is returning is a NaN. Which fundementally is an indefinite number. This is what most implementations return when its unable to convert the string to a date. Some implementations do not cope with anything but an RFC 1123 compliant date strings (which is all that the spec requires).

Edit: A comment to this answer states that Date.parse does not return NaN. However the spec says that parse should return a number. What number should it return when given a string that it can't parse as a date? It can't use 0 or -1 or some other such 'rogue' value because these a valid millisecond offsets from Jan 1, 1970. Mozilla and IE both return NaN which is a perfectly sensible thing to do.

Whilst the spec doesn't preclude the parsing of a string such as "2008-28-10" to a valid date it doesn't require it. I haven't come across any implementations that do anything more than required in the spec. Hence "Oct 10, 2008" is the closest you're gonna get to the string above that will parse properly.

AnthonyWJones
Date.parse certainly does not return a NaN.
ProfK
Try it. Give it "2008-28-10" as in the question and test with isNaN.
AnthonyWJones
Sorry, I was doing 'question box' coding and used the wrong date format. You could have pointed out that my particular Date.parse() was returning a Nan, though.
ProfK
Hmm... I thought I did. However I suggest you edit your question to use a valid date string. I can then delete this answer so that this question can stay on topic.
AnthonyWJones
+1  A: 

I can't answer in place of the language designers, but you can use the result of Date.parse or Date.UTC in the Date constructor to get such object.

Note that your code sample is incorrect: it is not a valid date format, not ISO (yyyy-mm-dd) nor IETF (Mon, 25 Dec 1995 13:30:00 GMT+0430 ). So you will get a NaN. Date.parse only understand IETF format, from what I have read on MDC.

If you need to compare two dates, you might compare the results of .getFullYear(), .getMonth() and .getDay(), or just compare the string representations at the wanted level.

var d1 = new Date();
var n = Date.parse("28 Oct 2008");
var d2 = new Date(n);
var d3 = new Date("28 october 2008");

alert(d1.toDateString() == d2.toDateString());
alert(d2.toDateString() == d3.toDateString());
PhiLho