为什么JS Date Difference Script中缺少大约30天 [英] Why is there an about 30 days missing in JS Date Difference Script

查看:104
本文介绍了为什么JS Date Difference Script中缺少大约30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习JavaScript和Jquery,我遇到了一个解决方案,可以相互减去2个日期。但结果是短短30天。怎么会?我做错了什么?另外,什么是最佳做法?

I am currently learning JavaScript and Jquery and I came across a solution to subtract 2 dates from each other. However, the result is short of 30 days. How come? What am I doing incorrectly? Also, what would be best practice?

<script>
    var d1 = new Date(); // todays date
    var d2 = new Date(2011,12,27);
    var msPerDay = 1000*60*60*24;
    document.write( ((d1 - d2) / msPerDay).toFixed(0) + " days");
</script>

提前致谢

推荐答案

Javascript月份从零开始。

Javascript months are zero-based.

12月 11

12 将环绕并成为明年的1月。

December is 11.
12 will wrap around and become January of the next year.

这篇关于为什么JS Date Difference Script中缺少大约30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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