为什么getDay返回不正确的值? (javascript) [英] Why does getDay return incorrect values? (javascript)

查看:261
本文介绍了为什么getDay返回不正确的值? (javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Javascript中的 Date 对象的 getDay()函数来获取一周的日子。
在理论上它工作正常,但有时输出有延迟,因此如果函数返回4,则返回1。

I tried to get the day of a week with the getDay() function of the Date object in Javascript. In theory it works fine, but sometimes there is a delay in the output, so if the function should return a "4", it returns a "1".

例如

var date= new Date("2009","04","30");
alert(date.getDay()); // the function returns 6, should return 4

var date= new Date("2009","05","01");
alert(date.getDay()); // the function returns 1, should return 5

我真的不知道为什么会发生这种情况。这个例子来自IE8。 FF 3的行为类似,但返回不同的值。第一个是7,而不是4.第二个就像在IE8中。

I really don't know why this happens. This example comes from IE8. FF 3 behaves similar, but returns different values. The first one is 7, not 4. The second one is just like in IE8.

任何想法为什么会发生这种情况?

Any ideas why this happens?

推荐答案

因为月数是零,不是一个。

Because the month number is zero based, not one based.

new Date 2009,04,30)可能的第30个创建 Date

(基于零的原因可能是历史性的,即它的行为与某种方法在不同的系统方式回溯到时间... )

(The reason why it's zero based is probably historic, i.e. it behaves the same as some method in a different system way back in time...)

这篇关于为什么getDay返回不正确的值? (javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆