Possible Duplicate:
Creating date with numbers (new Date(2012, 03, …) gives wrong month (+1)
I have this function:
function myFunction(){
var d = new Date(2012,9,1);
var x = document.getElementById("demo");//some element demo
x.innerHTML=d.getDay();
}
I am trying to get the weekday on 1 date of any month . The code is above(an example).
Isn’t this supposed to return a saturday that is 6 as its value? It doesn’t give correct value. It gives 1 in this case.It seems simple but i am missing something.
am i misinterpreting the equation ?
Please tell.
some edit: Although it is a little bit different but still it was on the same line.
function myFunction()
{
for(i=0;i<12;i++){
var d = new Date(2012,i,0);
k=i+1;
var x = document.getElementById(""+k);
x.innerHTML=d.getDate()+" "+i;
}
}
This function writes the particular ids 12 total in no. and i get the following output:
31 0
31 1
29 2
31 3
30 4
31 5
30 6
31 7
31 8
30 9
31 10
30 11
That is not correct.
The function above is generating out months and corresponding days.
You forgot that when creating a new
Dateobject, themonthparameter takes a number between 0-11. September is 8. Try changing that to 8, and your day will return6, which is Saturday.Your corrected code:
Demo