Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff() in "days" broken after making add() "days" aware of daylight-saving changes #384

Closed
prantlf opened this issue Oct 14, 2018 · 2 comments · Fixed by #453
Closed

diff() in "days" broken after making add() "days" aware of daylight-saving changes #384

prantlf opened this issue Oct 14, 2018 · 2 comments · Fixed by #453
Labels

Comments

@prantlf
Copy link
Contributor

prantlf commented Oct 14, 2018

Computing difference between two dates, which one was subtracted 1000 days from, returns 999 days, if the date is on the boundary of daylight-saving changes:

> d1 = dayjs('2018-10-28')
2018-10-27T22:00:00.000Z
> d2 = dayjs('2018-10-28').add(-1000, 'days')
2016-01-31T23:00:00.000Z
> d1.diff(d2, 'hours')
23999
> d1.diff(d2, 'days')
999

Moment.js computes the difference correctly. Notice, that the difference in hours is correct in Day.js too:

> m1 = moment('2018-10-28')
moment("2018-10-28T00:00:00.000")
> m1.toDate()
2018-10-27T22:00:00.000Z
> m2 = moment('2018-10-28').add(-1000, 'day')
moment("2016-02-01T00:00:00.000")
> m2.toDate()
2016-01-31T23:00:00.000Z
> m1.diff(m2, 'hours')
23999
> m1.diff(m2, 'days')
1000

This problem has been caused by 969aced. It adds/subtracts days using the local time zone. Previously, days were added/subtracted in UTC (using UNIX time), like hours and shorter units. While it helps people, who were not aware of Day.js working in the local time zone (#262 and #329), it also made Day.js inconsistent, because computing difference in days is still done in UTC (using UNIX time).

Once add() and subtract() methods were modified to expect the day count in the local time zone, the diff() method should be modified to return the day count in the local time zone too.

Adding/subtracting hours and shorter units happens in UTC, computing the difference in these units happens in UTC as well. Handling hours and shorter units by Day.js is consistent.

The tests were executed in Node.js 8 in the time zone "Europe/Prague" using Day.js 1.7.7 and Moment.js 2.22.2.

@slimCODE
Copy link

slimCODE commented Nov 2, 2018

Related: Adding one 'day' on a daylight-changing day is broken. It adds 24 hours, no matter if that day has 23, 24 or 25 hours.

let a = dayjs('2018-11-04');
console.log(a);
// h {$d: Sun Nov 04 2018 00:00:00 GMT-0400 (Eastern Daylight Time), $y: 2018, $M: 10, $D: 4, ...}
a = a.add(1, 'day');
console.log(a);
// h {$d: Sun Nov 04 2018 23:00:00 GMT-0500 (Eastern Standard Time), $y: 2018, $M: 10, $D: 4, …}

@iamkun
Copy link
Owner

iamkun commented Feb 2, 2019

🎉 This issue has been resolved in version 1.8.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun added the released label Feb 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants