473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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?
Jul 20 '05 #1
5 2745
da***@bi-tech.com (David Woodward) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
David Woodward wrote:
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/...e.html#1193137

Jul 20 '05 #3
LJL
da***@bi-tech.com (David Woodward) wrote in
news:2a*************************@posting.google.co m:
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
Jul 20 '05 #4
LJL <no**@nowhere.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
JRS: In article <ek**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Mon, 2 Feb 2004 19:13:31 :-
LJL <no**@nowhere.com> writes:
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)


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).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mick White | last post by:
According to the Safari browser the world began on "Fri Dec 13 1901 15:45:52 GMT-0500", but I need to be able to get around this limitation. I am interested in dates from 1500 to 1901, as far as...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
7
by: erekose666 | last post by:
I need a java prog to do the following: Create class Date with the following capabilities: a) Output the date in multiple formats, such as: MM/DD/YYYY June 14, 2005 DDD YYYY b) Use...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.