2011年10月29日至2011年10月31日期间出现奇怪错误,计算夜晚数错误
我使用两个日期选择器来获取到达和离开日期,计算天数和夜晚数,一切都运行良好,除了一件事:如果我将开始日期设置为 2011 年 10 月 29 日,结束日期设置为 2011 年 10 月 31 日 jquery 计数住了3晚,但只有2晚。只有十月份,其他月份仍然工作正常,希望有人能帮助我找出我的错误在哪里:
var oneDay = 1000*60*60*24;
var difference = Math.ceil((arrivalDate.getTime() - departureDate.getTime()) / oneDay);
i use two datepickers to get the arrival and the departure date, counting the days for the number of nights, all is working nice, except one thing: if i take my startdate on 29.10.2011 and my enddate on the 31.10.2011 jquery counts 3 nights but there only 2 nights. Only in october, other month still working fine, hope someone could help me to figure out where my mistake is:
var oneDay = 1000*60*60*24;
var difference = Math.ceil((arrivalDate.getTime() - departureDate.getTime()) / oneDay);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是欧洲的夏令时于 10 月 30 日结束(根据德国的错误消息,您所在的地方就是夏令时)。因此,日期之间通常有 24+24+24=72 小时,在本例中,日期之间有 24+24+24+1=73 小时。因此,您的代码计算出日期之间有 2.041666 天。然后,您的
Math.ceil
将其向上舍入为 3。在这种情况下,最简单的解决方案可能只是将
Math.ceil
替换为Math.round.当 DST 结束时,您的 2.04166 天将四舍五入为 2 天。当 DST 开始时,您将锻炼的 1.95834 天也将四舍五入为 2 天。
The problem is that daylight saving time ends on 30th October in Europe (where, going by the German error message, you are). As a result, where there would normally be 24+24+24=72 hours between the dates, in this case, there are 24+24+24+1=73 hours between the dates. Your code therefore works out that there are 2.041666 days between the dates. Your
Math.ceil
then rounds this up to 3.The simplest solution to this in this case is probably just to replace
Math.ceil
withMath.round
. When DST ends, your 2.04166 days will be rounded to 2 days. When DST starts, the 1.95834 days you'll work out then will also be rounded to 2 days.更改:
至:
change:
to:
三月不应该被搞乱,因为它只少了一个小时,所以它仍然可以四舍五入到 24 小时,如果我是对的?
问候拉尔夫
March should not get messed up, because it just one hour less, so it still gets round up to 24 hours, if i'm correct?
Greetings Ralf
以下内容正确地报告了 2 个“晚上”,或者实际上是 2 天。
我怀疑问题出在 Date 对象的时间方面。您可以通过删除时间元素来“清理”输入。
The following correctly reports 2 'nights' or in fact 2 days.
I suspect the problem is with the time aspect of the Date object. You could 'clean' the inputs by removing the time element thus.