Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with getDay() method javascript

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 !!!

like image 614
Geraud Mathe Avatar asked Nov 15 '10 18:11

Geraud Mathe


2 Answers

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());
like image 132
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Oct 03 '22 03:10

15ee8f99-57ff-4f92-890c-b56153


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());
like image 44
wajiw Avatar answered Oct 03 '22 03:10

wajiw