Javascript flaw
-
This is my blog entry about it: Weirdest Javascript bug ever[^] Essentially, if you do a 'var d=new Date(2005,2,27);' in javascript you will get the date 26/03/2005 in both IE and FireFox for Windows XP. I believe this to be an error in the way Windows handles dates, but Javascript should have taken this into account. Beat that!:cool:
---------- Siderite
-
This is my blog entry about it: Weirdest Javascript bug ever[^] Essentially, if you do a 'var d=new Date(2005,2,27);' in javascript you will get the date 26/03/2005 in both IE and FireFox for Windows XP. I believe this to be an error in the way Windows handles dates, but Javascript should have taken this into account. Beat that!:cool:
---------- Siderite
This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006
-
This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006
You didn't quite get it. The month is 0 based, I know that, but the date was offset by one day. And the reason that is working on your end is because you're not in Romania, where 27.03.2005 is a daylight saving day. I assure you that on my computer this is exactly what happens. Imagine trying to understand what was wrong in the code: C# ,SQL and Javascript and the problem to be something like this. To make it more clear: new Date(27,2,2005) returns 26 March 2005 23:00:00... But thanks, I didn't realize that this might never happen on computers set in other countries. We have a non standard daylight saving. I will update my blog.
---------- Siderite
-
This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006
TNCaver wrote:
Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is.
Probably because that's what
struct tm
in the C standard library does. Now, why that does it is a mystery. It's presumably been inherited from original UNIX.struct tm
is a real dog's dinner. I divine that you are in the UK. I think the other 'problem' is appearing because the argument for date's constructor is interpreted as UTC, whiletoLocalString
gives you the value in the local time zone.Stability. What an interesting concept. -- Chris Maunder
-
You didn't quite get it. The month is 0 based, I know that, but the date was offset by one day. And the reason that is working on your end is because you're not in Romania, where 27.03.2005 is a daylight saving day. I assure you that on my computer this is exactly what happens. Imagine trying to understand what was wrong in the code: C# ,SQL and Javascript and the problem to be something like this. To make it more clear: new Date(27,2,2005) returns 26 March 2005 23:00:00... But thanks, I didn't realize that this might never happen on computers set in other countries. We have a non standard daylight saving. I will update my blog.
---------- Siderite
-
Siderite Zaqwedex wrote:
We have a non standard daylight saving.
I'll say. What is a daylight saving day?
A day in which time is moved forward or backward with one hour. http://en.wikipedia.org/wiki/Daylight_saving[^]
---------- Siderite