This question already has an answer here:
这个问题在这里已有答案:
- Why this operation with date (number of days between 2 dates) return this value? 3 answers
- 为什么这个带日期的操作(两个日期之间的天数)会返回这个值? 3个答案
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2013, 06, 30);
var secondDate = new Date(2013, 07, 01);
var diffDays = Math.round(Math.abs((secondDate.getTime() - firstDate.getTime()) / (oneDay)));
I run the above code the answer should be 1day. But It is giving me 2days. Can you help me?
我运行上面的代码答案应该是1天。但它给了我2天。你可以帮我吗?
2 个解决方案
#1
4
That's because months are 0
indexed in JavaScript. So your first date is in July, and the second one in August.
这是因为在JavaScript中索引的月数为0。所以你的第一次约会是在七月份,第二次是在八月份。
You're comparing with a month having 31 days, hence the correct difference of 2 days.
您与31天的月份进行比较,因此正确的差异为2天。
When I enter dates this way in JavaScript, I explicitly add the offset so that other coders don't read it wrong, it's too easy to make this error :
当我在JavaScript中以这种方式输入日期时,我明确地添加了偏移量,以便其他编码器不会读错,这很容易造成这个错误:
var firstDate = new Date(2013, 06 - 1, 30); // -1 due to the months being 0-indexed in JavaScript
var secondDate = new Date(2013, 07 - 1, 01);
Yes I had had my code "fixed"...
是的,我的代码“已修复”......
#2
0
In javascript month is starting from 00-Jan since 05-june
在javascript月份从05年1月1日开始
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2013, 05, 30);
var secondDate = new Date(2013, 06, 01);
var diffDays = (secondDate- firstDate)/oneDay;
alert(diffDays);
To avoid confusion you can use like the following
为避免混淆,您可以像下面这样使用
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date('JUNE 30,2013');
var secondDate = new Date('JULY 01,2013');
var diffDays = (secondDate- firstDate)/oneDay;
alert(diffDays);
#1
4
That's because months are 0
indexed in JavaScript. So your first date is in July, and the second one in August.
这是因为在JavaScript中索引的月数为0。所以你的第一次约会是在七月份,第二次是在八月份。
You're comparing with a month having 31 days, hence the correct difference of 2 days.
您与31天的月份进行比较,因此正确的差异为2天。
When I enter dates this way in JavaScript, I explicitly add the offset so that other coders don't read it wrong, it's too easy to make this error :
当我在JavaScript中以这种方式输入日期时,我明确地添加了偏移量,以便其他编码器不会读错,这很容易造成这个错误:
var firstDate = new Date(2013, 06 - 1, 30); // -1 due to the months being 0-indexed in JavaScript
var secondDate = new Date(2013, 07 - 1, 01);
Yes I had had my code "fixed"...
是的,我的代码“已修复”......
#2
0
In javascript month is starting from 00-Jan since 05-june
在javascript月份从05年1月1日开始
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2013, 05, 30);
var secondDate = new Date(2013, 06, 01);
var diffDays = (secondDate- firstDate)/oneDay;
alert(diffDays);
To avoid confusion you can use like the following
为避免混淆,您可以像下面这样使用
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date('JUNE 30,2013');
var secondDate = new Date('JULY 01,2013');
var diffDays = (secondDate- firstDate)/oneDay;
alert(diffDays);