views:

526

answers:

1

I tried to get the day of a week with the getDay() function of the Date object in Javascript. In theory it works fine, but sometimes there is a delay in the output, so if the function should return a "4", it returns a "1".

E.g.

var date= new Date("2009","04","30");
alert(date.getDay()); // the function returns 6, should return 4

var date= new Date("2009","05","01");
alert(date.getDay()); // the function returns 1, should return 5

I really don't know why this happens. This example comes from IE8. FF 3 behaves similar, but returns different values. The first one is 7, not 4. The second one is just like in IE8.

Any ideas why this happens?

+6  A: 

Because the month number is zero based, not one based.

new Date("2009","04","30") creates a Date object for the 30th of may, not the 30th of april.

(The reason why it's zero based is probably historic, i.e. it behaves the same as some method in a different system way back in time...)

Guffa
I don't care what the reason is, it's not good enough. Zero-based months are STUPID. But we're stuck with them...sigh....
RolandTumble
Arrays are zero based, months are an array. Java and all other languages use zero too. I am guess you are stuck using STUPID languages until you code your own.
epascarello
Well, not all languages... The DateTime(int,int,int) constructor in .NET uses a one based month number.
Guffa
Thanks a lot, i hate Javascript for this kinda stupid things... -.-
Gushiken
Agree, the date APIs is not consistent at all. Months go from 0 to 11, days from 1 to 31 and a method like getDay returns a number between 0 to 6 and 0 means Sunday...
david