Skip to content Skip to sidebar Skip to footer

Javascript, GetDay() Returning Wrong Number

To start off, I know that the day of week in javascript starts at 0, Sunday = 0, Saturday = 6. However, there is something simple that I am missing here, but the following code alw

Solution 1:

If you create a date from a string be sure to specify the time:

var string = "2014-06-21 00:00:00";
var temp = new Date(string);
alert(temp.getDay());

You are probably getting the previous day because you are not specifying the time (in the date string). In this case, your current time zone will be used (mine is GMT-03h)

Another option is to create a date using the Date constructor which takes numbers as it's parameters:

new Date(year,month,day);

Or, in your case:

var temp = new Date(2014, 6, 21);
alert(temp.getDay());

Solution 2:

If you don't specify a time in your string it will default to your current time zone.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse


Post a Comment for "Javascript, GetDay() Returning Wrong Number"