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

Getting wrong startOf('day') in a different timezone #1212

Closed
nikhilag opened this issue Nov 12, 2020 · 3 comments · Fixed by #1229
Closed

Getting wrong startOf('day') in a different timezone #1212

nikhilag opened this issue Nov 12, 2020 · 3 comments · Fixed by #1229
Labels

Comments

@nikhilag
Copy link

Describe the bug
On calculating startOf('day') in a different timezone, I am getting the wrong result. Note that I tried this at 6 PM on November 12 in Asia/Kolkata timezone.
Code - dayjs.tz(new Date()).tz('America/New_York').startOf('day').toString()
Result - "Thu, 12 Nov 2020 15:30:00 GMT"

Expected behavior
It should be Thu, 12 Nov 2020 05:00:00 GMT

Information

  • Day.js Version - Tried in browser console on day.js.org website
  • OS - MacOS Mojave
  • Browser - Chrome 86.0.4240.193
  • Time zone: Asia/Kolkata
@nikhilag nikhilag changed the title Getting wrong startOf day in a different timezone Getting wrong startOf('day') in a different timezone Nov 12, 2020
@nbudin
Copy link
Contributor

nbudin commented Nov 15, 2020

I have a reproduction for this in the dayjs test suite, but I'm not sure how to fix it. Here is how I reproduced it in test/timezone.test.js:

it('startOf day across timezones', () => {
  const originalDay = dayjs('2010-01-01T00:00:00Z').tz('UTC')
  const startOfDay = originalDay.startOf('day')
  expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
})

(I also had to extend dayjs with the timezone plugin to make this run.)

I'm going to keep at this and see if I can come up with a fix, but if a maintainer has an idea I'd love to hear it since I'm not very familiar with the dayjs internals.

@nbudin
Copy link
Contributor

nbudin commented Nov 15, 2020

I meant to paste the output from this test, sorry about that. Here it is:

$ env TZ=America/Whitehorse yarn run test-tz
yarn run v1.22.10
$ date && jest test/timezone.test --coverage=false
Sun Nov 15 11:43:34 MST 2020
 FAIL  test/timezone.test.js
  ✓ Add Time days (DST) (9ms)
  ✓ Utc Offset (1ms)
  ✓ Diff (DST) (5ms)
  ✓ UTC add day in DST (2ms)
  ✓ UTC and utcOffset (1ms)
  ✓ UTC diff in DST (1ms)
  ✕ startOf day across timezones (13ms)

  ● startOf day across timezones

    expect(received).toEqual(expected)

    Expected value to equal:
      1262304000000
    Received:
      1262275200000

      77 |   const originalDay = dayjs('2010-01-01T00:00:00Z').tz('UTC')
      78 |   const startOfDay = originalDay.startOf('day')
    > 79 |   expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
      80 | })
      81 |

      at Object.<anonymous> (test/timezone.test.js:79:32)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 6 passed, 7 total
Snapshots:   0 total
Time:        0.823s, estimated 1s
Ran all test suites matching /test\/timezone.test/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@iamkun
Copy link
Owner

iamkun commented Dec 5, 2020

🎉 This issue has been resolved in version 1.9.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

3 participants