473,786 Members | 2,426 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 2771
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
3988
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 early year "types" as a number between 0 and 13.
12
29472
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
3956
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 integer values. In the second case it should receive a String and two integer values. In the third...
10
12448
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
9496
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
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
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.