473,545 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(2 004)
oDate.setMonth( 1)
oDate.setDate(1 )

iDay = oDate.getDay()

Is this a know problem or an expected result?
Jul 20 '05 #1
5 2756
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(2 004)
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(2 004,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/rasterTriangleD OM.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(2 004)
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.goog le.com:
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(2 004)
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.c om> 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.setFullY ear(2003,0,31);
myDate.setFullY ear(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
JRS: In article <ek**********@h otpop.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.c om> 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.setFullY ear(2003,0,31);
myDate.setFullY ear(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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
3966
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 I can determine, there are 14 possible calendar variations. Year starts on Sun, Mon..... Leap year starts on Sun, Mon.. I can label these...
12
29427
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
7
3941
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 overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three...
10
12427
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 =1;} else {memDayHol = memDayHol - 1;} }
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5344
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.