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 for same day (one UTC, one regular) gives 10800000 #926

Closed
mariusa opened this issue Jun 7, 2020 · 7 comments · Fixed by #995
Closed

diff for same day (one UTC, one regular) gives 10800000 #926

mariusa opened this issue Jun 7, 2020 · 7 comments · Fixed by #995
Labels
☢️Bug Something isn't working released

Comments

@mariusa
Copy link

mariusa commented Jun 7, 2020

Describe the bug
UTC diff for same day gives 10800000

let dayjs = require('dayjs')
let utc = require('dayjs/plugin/utc')
dayjs.extend(utc)

console.log('diff', dayjs.utc('2021-06-07').diff('2021-06-07', 'days')) //gives 10800000. Expected: 0 or maybe 1, if timezone difference
console.log('diff', dayjs.utc('2021-06-07').diff(dayjs.utc('2021-06-07'), 'days')) //gives 0, correct

Expected behavior
diff = 0 or maybe 1, if timezone difference

Information

  • Day.js Version 1.8.27
  • OS: Fedora
  • Browser nodejs 12
  • Time zone: GMT+3
@iamkun
Copy link
Owner

iamkun commented Jun 7, 2020

This should give you the correct result

dayjs.utc('2021-06-07').diff(dayjs.utc('2021-06-07'), 'days')

Cause you are comparing one UTC time with one local time above.

@mariusa
Copy link
Author

mariusa commented Jun 7, 2020

Right, I've mentioned the 2nd line works properly.

But in the 1st case, should the diff be 0 or maybe 1, if timezone difference?
There can't be 10800000 days difference between same date in 2 different timezones.

@iamkun
Copy link
Owner

iamkun commented Jun 8, 2020

Apparently, there's a bug in calculating diff between two different timezones.

@iamkun iamkun added the ☢️Bug Something isn't working label Jun 8, 2020
@iamkun
Copy link
Owner

iamkun commented Jun 8, 2020

Current workaround.

dayjs.utc('2021-06-07').local().diff('2021-06-07', 'days')) 
dayjs('2021-06-07').diff(dayjs.utc('2021-06-07').local(), 'days')) 

@iamkun
Copy link
Owner

iamkun commented Aug 8, 2020

fixed

@iamkun
Copy link
Owner

iamkun commented Aug 10, 2020

🎉 This issue has been resolved in version 1.8.33 🎉

The release is available on:

Your semantic-release bot 📦🚀

@justingolden21
Copy link

justingolden21 commented Feb 22, 2022

How is one supposed to calculate the difference between two timezones? Get the UTC string with format 'Z' and then parse int?

EDIT: got it with .utcOffset() / 60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️Bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants