2011年10月29日至2011年10月31日期间出现奇怪错误,计算夜晚数错误

发布于 2024-12-05 14:16:52 字数 304 浏览 1 评论 0原文

我使用两个日期选择器来获取到达和离开日期,计算天数和夜晚数,一切都运行良好,除了一件事:如果我将开始日期设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

临走之时 2024-12-12 14:16:52

问题是欧洲的夏令时于 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 with Math.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.

半寸时光 2024-12-12 14:16:52

更改:

var difference = Math.ceil((arrivalDate.getTime() - departureDate.getTime()) / oneDay); 

至:

var different = Math.Floor(arrivalDate.getTime() / oneDay) - Math.Floor(departureDate.getTime() / oneDay)

change:

var difference = Math.ceil((arrivalDate.getTime() - departureDate.getTime()) / oneDay); 

to:

var different = Math.Floor(arrivalDate.getTime() / oneDay) - Math.Floor(departureDate.getTime() / oneDay)
不忘初心 2024-12-12 14:16:52

三月不应该被搞乱,因为它只少了一个小时,所以它仍然可以四舍五入到 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

囚你心 2024-12-12 14:16:52

以下内容正确地报告了 2 个“晚上”,或者实际上是 2 天。

var startDate = new Date(2011,10,29);
var endDate = new Date(2011,10,31);
var oneDay = 1000*86400;
var difference = Math.ceil((endDate.getTime() - startDate.getTime())/oneDay);
alert(difference);

我怀疑问题出在 Date 对象的时间方面。您可以通过删除时间元素来“清理”输入。

function JustDate(inputDate) {
    return new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
}

var departureDate = JustDate(departure.datepicker("getDate"));
var arrivalDate = JustDate(arrival.datepicker("getDate"));

The following correctly reports 2 'nights' or in fact 2 days.

var startDate = new Date(2011,10,29);
var endDate = new Date(2011,10,31);
var oneDay = 1000*86400;
var difference = Math.ceil((endDate.getTime() - startDate.getTime())/oneDay);
alert(difference);

I suspect the problem is with the time aspect of the Date object. You could 'clean' the inputs by removing the time element thus.

function JustDate(inputDate) {
    return new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
}

var departureDate = JustDate(departure.datepicker("getDate"));
var arrivalDate = JustDate(arrival.datepicker("getDate"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文