Date object's getDay returns wrong date for Feb in any leap year

D

David Woodward

When I do the following (the date represents 2/1/2004) getDay returns
1, and it should be returning 0. It's the only month for which we see
this behavior.

var oDate = new Date()
oDate.setYear(2004)
oDate.setMonth(1)
oDate.setDate(1)

iDay = oDate.getDay()

Is this a know problem or an expected result?
 
L

Lasse Reichstein Nielsen

When I do the following (the date represents 2/1/2004)

Which date is that? Second of January or first of February? (Yes, I
know, but only because your remaning text is less ambiguous than this
explanatory note :)

Did you try it three days ago? It would have worked then.
getDay returns
1, and it should be returning 0. It's the only month for which we see
this behavior.

Tomorrow (on the 31th) you will see it for all 30 day months too.

Have you tried debugging at all? Inserting alert statments between
each line to see the current value of oDate? It would be informative.
var oDate = new Date()

Here oDate is 31th of January 2004 for me now.
oDate.setYear(2004)

No change
oDate.setMonth(1)

Now you set the month to February. That means that the date is now
supposed to be the 31th of February 2004 . Since that doesn't exist,
the Date-object autocorrects it to the 2rd of March 2004 (two days
after the 29th of February)
oDate.setDate(1)

Now it is 1st of March 2004 .
iDay = oDate.getDay()

1st of March 2004 is a Monday.
Is this a know problem or an expected result?

Expected result. Never assign parts of a date separatly, especially
date in month and month. Use
oDate.setMonth(1,1);
or better yet
oDate.setYear(2004,1,1);
(better because it won't fail on the 29th of February, like, e.g.,
var myDate = new Date(2003,1,1); // some date in 2003
myDate.setMonth(1,29);
myDate.setYear(2004);
result: 1st of March 2004)

/L
 
E

ExGuardianReader

David said:
When I do the following (the date represents 2/1/2004) getDay returns
1, and it should be returning 0. It's the only month for which we see
this behavior.

var oDate = new Date()
oDate.setYear(2004)
oDate.setMonth(1)
oDate.setDate(1)

iDay = oDate.getDay()

Is this a know problem or an expected result?

Month values are zero based. January = 0, February = 1.

Read the documentation:
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html#1193137
 
L

LJL

(e-mail address removed) (David Woodward) wrote in
When I do the following (the date represents 2/1/2004) getDay returns
1, and it should be returning 0. It's the only month for which we see
this behavior.

var oDate = new Date()
oDate.setYear(2004)
oDate.setMonth(1)
oDate.setDate(1)

iDay = oDate.getDay()

Is this a know problem or an expected result?

It's not a problem, but it does have a fix that is fairly easy.
When setting Year, Month and Date, do it in reverse, ie. Date, Month, Year.
That should fix the problem, as long as you don't try to setDate() to 31 in
a month with only 30 days.

If you get the date new Date(), and the date is 30 or 31, then you set the
month to February, which has only 28 or 29 days, the system tries to figure
it out before you set the date.

So, simply change the order of how you set the information.

Try doing the same thing on the 31st of January. Set the month to any
other month with 30 days or fewer. Get the month. It will often come back
as a different month than what you had set.

I found out the hard way.

Good luck, LJL
 
L

Lasse Reichstein Nielsen

LJL said:
It's not a problem, but it does have a fix that is fairly easy.

.... although not very general.
When setting Year, Month and Date, do it in reverse, ie. Date, Month, Year.
That should fix the problem, as long as you don't try to setDate() to 31 in
a month with only 30 days.

Exactly. No ordering will work all the time, so it is *far* better to
set them all at the same time.
myDate.setFullYear(2003,0,31);
myDate.setFullYear(2004,1,29);
(or setYear if you don't have setFullYear)

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
... although not very general.


Exactly. No ordering will work all the time, so it is *far* better to
set them all at the same time.
myDate.setFullYear(2003,0,31);
myDate.setFullYear(2004,1,29);
(or setYear if you don't have setFullYear)

On the Web, it is the reader, not the author, who has to have
setFullYear.

If there is doubt about setFullYear, note that ECMA-262 3rd Edn does not
require setYear to accept M or D.

One can always setDate(1), setMonth(M), setYear(Y), setDate(D).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top