最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - Why does getDay return incorrect values? (javascript) - Stack Overflow

programmeradmin1浏览0评论

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".

E.g.

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

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?

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".

E.g.

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

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?

Share Improve this question edited May 7, 2009 at 14:09 Rob Kennedy 163k23 gold badges284 silver badges477 bronze badges asked May 7, 2009 at 13:51 SvenFinkeSvenFinke 1,2543 gold badges15 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

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

new Date("2009","04","30") creates a Date object for the 30th of may, not the 30th of april.

(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...)

发布评论

评论列表(0)

  1. 暂无评论