简体   繁体   English

日期计算的奇怪错误

[英]Strange bug with date calculation

I have to calculate how many years passed between two dates. 我必须计算两个日期之间经过多少年。 My code works fine - except for very specific dates. 我的代码工作正常 - 除了非常具体的日期。

The code bellow should produce the same year difference for both dateEnd and dateEnd1 , however they are not equal. 下面的代码应该为dateEnddateEnd1产生相同的年份差异,但它们不相等。 If I would change year from 2003 to 2004, it would become equal. 如果我从2003年到2004年改变年份,它将变得平等。 If I would leave the year, but change from march to february, it would again be equal. 如果我要离开这一年,但从三月到二月,它将再次平等。

I guess it has something to do with leap years, but I am clueless why? 我想这与闰年有关,但我为何无能为力? Especially since there were 4 leap years in that period, but the year miscalculation happens for dates which are one day apart only. 特别是因为那个时期有4个闰年,但是这一年的错误计算只发生在相隔一天的日期。

I expect year difference to be 12 in both of these cases, however I get 13 in the first case with the dateEnd variable 我希望在这两种情况下年差为12,但是在第一种情况下我使用dateEnd变量得到13

var dateStart = '2003,03,11';
var dateEnd = '2016,03,10';
var dateEnd1 = '2016,03,09';

var difference = (new Date(dateEnd)).getTime() - (new Date(dateStart)).getTime();
difference = (new Date(difference)).getFullYear() - 1970;
alert('Between ' + dateStart + ' and ' + dateEnd + ' elapsed ' + difference + ' years.');
difference = (new Date(dateEnd1)).getTime() - (new Date(dateStart)).getTime();
difference = (new Date(difference)).getFullYear() - 1970;
alert('Between ' + dateStart + ' and ' + dateEnd1 + ' elapsed ' + difference + ' years.');

Here is the code on jsfiddle . 这是jsfiddle上的代码。 One person in the comments managed to reproduce the problem on his jsfiddle ! 评论中的一个人设法在他的jsfiddle上重现了这个问题!

Here are values of variables I get 这是我得到的变量的值

dateStart time: 1047337200000
dateEnd time: 1457564400000
dateEnd1 time: 1457478000000
Time difference for the first case: 410227200000
Between 2003,03,11 and 2016,03,10 elapsed 13 years.
Time difference for the second case: 410140800000
Between 2003,03,11 and 2016,03,09 elapsed 12 years.

This is not related to the locale. 这与语言环境无关。 I also get the 12 / 13 result when I change the dates to any other month in 2016, except January or February. 当我将日期更改为2016年的任何其他月份(1月或2月除外)时,我也会得到12/13的结果。 So the reason is that 2016 is a leap year and 2003 is not. 所以原因是2016年是闰年,2003年不是。

This proves that it is not reliable to calculate date differences as dates. 这证明将日期差异计算为日期是不可靠的。 In order to calculate the difference in years, you need to use the year parts of both dates. 为了计算年份的差异,您需要使用两个日期的年份部分。 Also compare the month parts, if the end month is before the start month then subtract one year and if start month and end month are the same then compare the day of the month too. 同时比较月份部分,如果结束月份在开始月份之前,则减去一年,如果开始月份和结束月份相同,则也比较月份日期。 You could also include hours, minutes, seconds and millis, if required. 如果需要,您还可以包括小时,分钟,秒和毫秒。

The can/cannot reproduce difference is not the locale, but the time zone. 可以/不能重现差异不是语言环境,而是时区。 For me (in California), the above values give: 对我(在加利福尼亚州),上述值给出:

alert(new Date(410140800000)); // "Thu Dec 30 1982 16:00:00 GMT-0800 (Pacific Standard Time)"
alert(new Date(410227200000)); // "Fri Dec 31 1982 16:00:00 GMT-0800 (Pacific Standard Time)"

Were I at or east of GMT, the second date would be "Sat Jan 1 1983...", giving the different number of years. 如果我在格林威治标准时间或东部,第二个日期将是“1983年1月1日星期一......”,给出不同的年数。 Leap years are probably coming into it because the number of leap years is affecting the number of days in the difference. 闰年可能会进入它,因为闰年​​的数量正在影响差异的天数。

I thought you would see the same thing if you use 2005/2018, but as Arjan points out, it's not quite that simple and does indeed involve leap years. 如果你使用2005/2018,我认为你会看到同样的事情,但正如Arjan指出的那样,它并不那么简单,确实涉及闰年。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM