考虑夏时制计算两个日期之间的差 [英] Calculate difference between 2 dates considering Daylight Saving Time

查看:102
本文介绍了考虑夏时制计算两个日期之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出开始日期和天数,我需要显示结束日期=开始日期+天数。

Given a start date, and a number of days, I need to display the end date = start date + number of days.

所以我做了这样的事情:

So I did something like this:

var endDate=new Date(startDate.getTime()+ONE_DAY);

一切正常,除了10月25日至26日少一天。

Everything works fine, except that for 25 and 26 October gives one day less.

例如:

2014-01-01 + 2 days = 2014-01-03

2014-10-25 + 2 days = 2014-10-26 (here is the case I need to treat).

出现这种差异是因为时钟要倒退1小时。实际上 2014-10-27 00:00:00 变为 2014-10-26 23:00:00

This difference appear because of the clock going back 1 hour. Practically 2014-10-27 00:00:00 becomes 2014-10-26 23:00:00.

一个简单的解决方案是在另一个小时(例如凌晨3点)进行计算。但是我只想在发生这种情况时显示一个注释。

A simple solution would be to compute this at another hour (example 3 AM). But I want to just display a note when this happens.

例如,如果用户输入 2014-10-25 ,我显示一个弹出窗口,说[something]。

For example, if user inputs 2014-10-25, I show a popup saying [something].

现在这才是真正的问题...我似乎找不到任何能说明时钟返回的算法。

Now here is the real problem... I can't seem to find any algorithm that says when clocks goes back in year X.

示例...在2014年,日期是10月26日。在2016年10月30日( https://www.gov.uk/when-do -the-clocks-change )。为什么?这个日期看起来是随机的,但我认为不是。那么...时钟什么时候后退/前进?

Example... in 2014 the day is 26 October. In 2016 is 30 October (https://www.gov.uk/when-do-the-clocks-change). Why? This date looks random to be, but I don't think it is. So... when does clock go back/forward?

编辑:所有答案/评论都与解决问题有关。但是...我已经过了那个阶段。现在,我只对计算时钟改变的日子到底是怎么回事?一事发

All answers/comments are helpful related to how to fix the problem. But... I already passed that stage. Now I only have an itch about "how on earth are the days when clock is changed computed?".

推荐答案

整天中两个日期之间的差,创建Date对象,从另一个对象中减去一个,然后在一天内四舍五入除以毫秒。其余时间仅会节省1小时以进行夏时制,因此将舍入到正确的值。

To find the difference between two dates in whole days, create Date objects, subtract one from the other, then divide by the milliseconds in one day and round. The remainder will only be out by 1 hour for daylight saving so will round to the right value.

您可能还需要一个小的函数,将字符串转换为Dates:

You may also need a small function to convert strings to Dates:

// Return Date given ISO date as yyyy-mm-dd
function parseISODate(ds) {
  var d = ds.split(/\D/);
  return new Date(d[0], --d[1], d[2]);
}

求天数差:

function dateDiff(d0, d1) {
  return Math.round((d1 - d0)/8.64e7);
}

// 297
console.log(dateDiff(parseISODate('2014-01-01'), parseISODate('2014-10-25')));

如果要在日期中添加天数,请执行以下操作:

If you want to add days to a date, do something like:

// Add 2 days to 2014-10-25
var d = new Date(2014, 9, 25);
d.setDate(d.getDate() + 2);

console.log(d);   // 2014-10-27

内置Date对象考虑了夏令时(是某些浏览器中的错误)。

The built–in Date object takes account of daylight saving (thought there are bugs in some browsers).

这篇关于考虑夏时制计算两个日期之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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