11

I'm trying to get the day name in javascript. Every time I search for usage of the function getDay(), it is explained that this method returns the day of the week, for example: 0 is sunday, 1 is monday etc.

So the 1st janauary 2010 was a friday, can someone explain why i'm getting 1 instead of 5? The same for 2nd janauary 2010, i'm getting 2 instead of 5.

I've tried some ways to do that without success.

Here's my code :

theDay = new Date(2010,01,01);  
alert(theDay.getDay());

Thank You !!!

Stephen
  • 18,827
  • 9
  • 60
  • 98
Geraud Mathe
  • 711
  • 2
  • 9
  • 20

2 Answers2

14

The month in JS is zero-based, just like the day of the week.

Date(2010,01,01) is 1 February, 2010. January is month zero. Sure enough, 1 February 2010 was a Monday (I remember it well).

Try this:

var theDay = new Date(2010,00,01);  
alert(theDay.getDay());
8

The month starts at 0, so what you're doing is trying to find Feb 1st, 2010 which is a Monday. This would be correct:

theDay = new Date(2010,0,01);  
alert(theDay.getDay());
wajiw
  • 12,239
  • 17
  • 54
  • 73