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

Wrong diff for DST in UTC mode #1097

Closed
jkondela opened this issue Sep 30, 2020 · 5 comments · Fixed by #1171
Closed

Wrong diff for DST in UTC mode #1097

jkondela opened this issue Sep 30, 2020 · 5 comments · Fixed by #1171
Labels

Comments

@jkondela
Copy link

Describe the bug
When using utc and one date is in DST while another is in non-DST, diff returns wrong value.
Diff for hours is correct, only days diff is affected.

For comparison, moment works as expected.

I noticed that diff is using timezone delta, but when dayjs is in UTC mode, the delta is 0 which is probably causing this bug.
https://github.com/iamkun/dayjs/blob/dev/src/index.js#L314

Maybe the same problem as here #1021

const dayjsD1 = dayjs.utc("2020-10-31"); // non DST date
const dayjsD2 = dayjs.utc("2020-10-31").add(-100, "days"); // DST
console.log(dayjsD1.diff(dayjsD2, "days"))
// prints 99


const momentD1 = moment.utc("2020-10-31");
const momentD2 = moment.utc("2020-10-31").add(-100, "days");
console.log(momentD1.diff(momentD2, "days"))
// prints 100

Expected behavior
Subtract 100 days from the original date should return diff 100 days.

Information

  • Day.js Version v1.9.1
  • OS: Windows 10
  • Browser Chrome 85, Firefox 81
  • Time zone: GMT+2 DST Bratislava (Central European Summer Time)

Thanks for the excellent date library.

@LiPinghai
Copy link

+1

@pavel-klavik
Copy link

+1, noticed this problem due to recent DST change in Central Europe. The number of hours is computed correctly by diff, so I can use it as a workaround.

const day1 = dayjs.utc("20201023");
const day2 = dayjs.utc("20201026");
day1.diff(day2, "d"); // incorrect 2
day1.hour(day2, "h"); // correct 72

@iamkun
Copy link
Owner

iamkun commented Oct 27, 2020

fixed, thanks.

iamkun added a commit that referenced this issue Oct 30, 2020
iamkun pushed a commit that referenced this issue Nov 5, 2020
## [1.9.5](v1.9.4...v1.9.5) (2020-11-05)

### Bug Fixes

* customParseFormat plugin supports parsing localizedFormats  ([#1110](#1110)) ([402b603](402b603))
* fix customParseFormat plugin parse meridiem bug ([#1169](#1169)) ([9e8f8d9](9e8f8d9)), closes [#1168](#1168)
* fix devHelper error in umd bundle in browser ([#1165](#1165)) ([d11b5ee](d11b5ee))
* fix utc plugin diff bug in DST ([#1171](#1171)) ([f8da3fe](f8da3fe)), closes [#1097](#1097) [#1021](#1021)
* isoWeek plugin type ([#1177](#1177)) ([c3d0436](c3d0436))
* update localeData plugin to support meridiem ([#1174](#1174)) ([fdb09e4](fdb09e4)), closes [#1172](#1172)
* update timezone plugin parse Date instance / timestamp logic & remove useless test ([#1183](#1183)) ([a7f858b](a7f858b))
@iamkun
Copy link
Owner

iamkun commented Nov 5, 2020

🎉 This issue has been resolved in version 1.9.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun added the released label Nov 5, 2020
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this issue May 10, 2022
## [1.9.5](iamkun/dayjs@v1.9.4...v1.9.5) (2020-11-05)

### Bug Fixes

* customParseFormat plugin supports parsing localizedFormats  ([#1110](iamkun/dayjs#1110)) ([402b603](iamkun/dayjs@402b603))
* fix customParseFormat plugin parse meridiem bug ([#1169](iamkun/dayjs#1169)) ([9e8f8d9](iamkun/dayjs@9e8f8d9)), closes [#1168](iamkun/dayjs#1168)
* fix devHelper error in umd bundle in browser ([#1165](iamkun/dayjs#1165)) ([d11b5ee](iamkun/dayjs@d11b5ee))
* fix utc plugin diff bug in DST ([#1171](iamkun/dayjs#1171)) ([f8da3fe](iamkun/dayjs@f8da3fe)), closes [#1097](iamkun/dayjs#1097) [#1021](iamkun/dayjs#1021)
* isoWeek plugin type ([#1177](iamkun/dayjs#1177)) ([c3d0436](iamkun/dayjs@c3d0436))
* update localeData plugin to support meridiem ([#1174](iamkun/dayjs#1174)) ([fdb09e4](iamkun/dayjs@fdb09e4)), closes [#1172](iamkun/dayjs#1172)
* update timezone plugin parse Date instance / timestamp logic & remove useless test ([#1183](iamkun/dayjs#1183)) ([a7f858b](iamkun/dayjs@a7f858b))
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this issue May 10, 2022
## [1.9.5](iamkun/dayjs@v1.9.4...v1.9.5) (2020-11-05)

### Bug Fixes

* customParseFormat plugin supports parsing localizedFormats  ([#1110](iamkun/dayjs#1110)) ([402b603](iamkun/dayjs@402b603))
* fix customParseFormat plugin parse meridiem bug ([#1169](iamkun/dayjs#1169)) ([9e8f8d9](iamkun/dayjs@9e8f8d9)), closes [#1168](iamkun/dayjs#1168)
* fix devHelper error in umd bundle in browser ([#1165](iamkun/dayjs#1165)) ([d11b5ee](iamkun/dayjs@d11b5ee))
* fix utc plugin diff bug in DST ([#1171](iamkun/dayjs#1171)) ([f8da3fe](iamkun/dayjs@f8da3fe)), closes [#1097](iamkun/dayjs#1097) [#1021](iamkun/dayjs#1021)
* isoWeek plugin type ([#1177](iamkun/dayjs#1177)) ([c3d0436](iamkun/dayjs@c3d0436))
* update localeData plugin to support meridiem ([#1174](iamkun/dayjs#1174)) ([fdb09e4](iamkun/dayjs@fdb09e4)), closes [#1172](iamkun/dayjs#1172)
* update timezone plugin parse Date instance / timestamp logic & remove useless test ([#1183](iamkun/dayjs#1183)) ([a7f858b](iamkun/dayjs@a7f858b))
@robozb
Copy link

robozb commented Aug 24, 2023

I experienced subtract error when I use timezones, an example:

import dayjs from 'dayjs'; 
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'

dayjs.extend(utc); 
dayjs.extend(timezone);

const dt1=dayjs().tz("Atlantic/Reykjavik")
console.log(dt1.format("YYYY-MM-DD HH:mm"))

const dt2=dt1.subtract(1,"day")
console.log(dt2.format("YYYY-MM-DD HH:mm"))

Result:

2023-08-24 00:38
2023-08-22 22:38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants