开发者

Javascript getDay returning incorrect values for April, June, September, November

I'm using this script located here: http://www.javascriptkit.com/script/script2/dyndateselector.shtml

If you try it, and go to any of April, June, September or November, you will notice that the day of the week columns are incorrect. Here's a list of incorrect data (the x starts y stuff is showing the following month.)

Bugged months: 4/April (starts Sunday instead of Friday) May starts Sunday

6/June (starts Friday instead of Wednesday) July starts Friday

9/September (starts Saturday instead of Thursday) October starts Saturday

11/November (starts Thursday instead of Tuesday) December starts Thursday

You'll notice that every bugged month is starting with the day of the following month, yet all the other months seem to be correct.

I can't find anything on this problem. Anyone able to help? The actual Javascript alone can be found here, and the getDay() method occurs on line 125: http://pastebin.com/0zuBYrzv

I've tested in both Firefox and Chrome.

Here's some very simple code to demonstrate the issue:

<script>
var d = new Date();
d.setMonth(5);
d.setFullYear(2011);
d.setDate(1);
alert(d.getDay());
</script>

This will create an alert with the message "5", meaning Friday (5+1 = 6, Friday is the 6th day of the week,) when in fact 开发者_高级运维Wednesday is the start of the week.


This is actually pretty interesting as i am guessing that tomorrow your original code will work as you want again.

What i think is happening is you are creating a new Date and that will automaticly initialize to today (31th of may). Then you set the Month to June by which you basically say make it 31th of June. This date doesn't exist so javascript will turn it into 1th of July. Finally you set the Date but since your month is not anymore what you want it to be the results will be wrong.


Looks like 0 is january and 11 is december.


Apparently JavaScript doesn't like it if I set the month, full year, then day. What I must do is set them all in one function, like so:

<script>
var d = new Date();
d.setFullYear(2011, 5, 1);
alert(d.getDay());
</script>


I think this is a bug in Javascript's Date Object. Please take a look at the following code. (I'm using w3school's JSref tool online to see it quickly.) Please note, the ways used below are told by w3 schools themselves.

Some examples of instantiating a date:

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24) (//THIS IS WRONG - unexpected results!!!)
var d3 = new Date(79,5,24,11,33,0)

So please be careful when using this Date object, looks like certain ways of instantiating dates are better than others.

<script type="text/javascript">

function whatDayIsToday(date)
{
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

document.write("Today is " + weekday[date.getDay()] + ", <br />");
document.write("the " + date.getDay() + getSuffix(date.getDay()) + " day of the week. <br /><br />") 
}
function getSuffix(num)
{
return (num>3)?"th":(num==3)?"rd":(num==2)?"nd":(num==1)?"st":"";
}

//CORRECT
var d3 = new Date("01/01/2011");
whatDayIsToday(d3);

//CORRECT
var d2 = new Date("01/01/2011");
whatDayIsToday(d2);

//DOESN'T WORK
var d5 = new Date("1-1-2011");
whatDayIsToday(d5);

//WRONG
var d4 = new Date("2011", "01", "01");
whatDayIsToday(d4);

//DAY OF WEEK IS WRONG
var d = new Date(2011, 1, 1);
whatDayIsToday(d);

//DAY OF WEEK IS ALSO WRONG
var d0 = new Date(11, 1, 1);
whatDayIsToday(d0);

</script>

outputs (all using some format of 1/1/2011) are:

Today is Saturday,
the 6th day of the week. (CORRECT, January first this year was a saturday)

Today is Saturday,
the 6nd day of the week.  (CORRECT)

Today is undefined,
the NaNnd day of the week. (WRONG FORMATTING, DOESN'T WORK - EXPECTED)

Today is Tuesday,
the 2nd day of the week.  (WRONG - UNEXPECTED)

Today is Tuesday,
the 2nd day of the week.  (WRONG - UNEXPECTED)

Today is Wednesday,
the 3nd day of the week.  (WRONG - UNEXPECTED)

Based on the other answers to this question, I'm guessing it has to do with the day I'm on currently (8/26/2011) - this would be my starting new Date() and the days and/or years getting applied in the wrong order. However, it sure would be nice if this thing worked!


This is the typical wrong approach:

var days = ["Söndag", "Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag"];
var d = new Date();
d.setFullYear(selectYear.options[selectYear.selectedIndex].value);
d.setMonth(selectMonth.options[selectMonth.selectedIndex].value-1);
d.setDate(selectDay.options[selectDay.selectedIndex].value);
alert(days[d.getDay()]);

It will work most of the time. But it will have problems with when current date is 31 and when trying to set date to 31 for when set at a month with less than 31 days.

This is a good way:

var days = ["Söndag", "Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag"];
var d = new Date(0); // solves all problems for existing dates
d.setFullYear(selectYear.options[selectYear.selectedIndex].value);
d.setMonth(selectMonth.options[selectMonth.selectedIndex].value-1);
d.setDate(selectDay.options[selectDay.selectedIndex].value);
if( d.getDate() == selectDay.options[selectDay.selectedIndex].value ) {
    alert(days[d.getDay()]);
}
else {
    alert("this date doesn't exist"); // date was changed cuz date didn't exist
}

Constructing the date with new Date(0) sets the date to 1 jan, this is good because the date is not 31st and january has 31 days which give us the space we need. It wont fail on the 31st and we don't need to set the datas in any special order. It will always works.

If the user sets month to september (30 days) and date to 31 the data object will hold either 1 oct or 1 sep (depending on which order you set them in). Which is irrelevant because they are both wrong. A simple check if the date is what we set it to will tell us if the date is a existing date, if not we can tell the user this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜