-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 behaviour of isBefore when using timezones #2182
Comments
Having a similar issue when testing dates across a DST boundary. Here's an example: d1 = dayjs.tz("2022-11-06T06:40:00.000Z", "America/Los_Angeles");
d2 = dayjs.tz("2022-11-06T07:10:00.000Z", "America/Los_Angeles");
d1.isBefore(d2)
> true
d2.isBefore(d1)
> true |
Hi @addiebarron, @colq2 👋 On all plugin/timezone issues, most of the time the solution is to set the date before setting the timezone and then manipulate the date (add, subtract, ...). Another solution, more for the example of @addiebarron, the pull requests #2118 solves a number of problems on the plugin/timezone. dayjs.tz('2023-02-10T00:00:00.000Z', 'Europe/Berlin')
// or
dayjs(new Date(2023, 2, 10)).tz('Europe/Berlin')
// or
dayjs()
.year(2023).month(3)
.date(10)
.hour(0)
.minute(0)
.second(0)
.millisecond(0)
.tz('Europe/Berlin') Hopefully this will solve your question, if so, don't forget to close this issue 😉 Testsif you want the tests describe('issue 2182 - after timezone set', () => {
// colq2 e.g.
const d1 = dayjs().tz('Europe/Berlin')
.year(2023).month(3)
.date(10)
.hour(0)
.minute(0)
.second(0)
.millisecond(0)
const d2 = d1.subtract(1, 'minute')
it('sould d2 be before d1', () => {
expect(d2.isBefore(d1)).toBe(true)
expect(d1.isBefore(d2)).toBe(false)
}) // failed
// const d1s = dayjs(new Date(2023, 2, 10)).tz('Europe/Berlin')
const d1s = dayjs.tz('2023-02-10T00:00:00.000Z', 'Europe/Berlin')
const d2s = d1s.subtract(1, 'minute')
it('sould d2 be before d1, solve ?', () => {
expect(d2s.isBefore(d1s)).toBe(true)
expect(d1s.isBefore(d2s)).toBe(false)
}) // passed
// addiebarron e.g.
const d1a = dayjs.tz('2022-11-06T06:40:00.000Z', 'America/Los_Angeles')
const d2a = dayjs.tz('2022-11-06T07:10:00.000Z', 'America/Los_Angeles')
it('sould d2 be before d1', () => {
expect(d1a.isBefore(d2a)).toBe(true)
expect(d2a.isBefore(d1a)).toBe(false)
}) // failed on dev, but passed on pr/2118
const d1as = dayjs(new Date(2022, 11, 6, 6, 40)).tz('America/Los_Angeles')
const d2as = dayjs(new Date(2022, 11, 6, 7, 10)).tz('America/Los_Angeles')
it('sould d2 be before d1, solve ?', () => {
expect(d1as.isBefore(d2as)).toBe(true)
expect(d2as.isBefore(d1as)).toBe(false)
}) // passed
}) |
Describe the bug
Comparing two dayjs instances in a specific timezone wich are 1 minute apart a before each other.
This is not happening when you are not using a timezone:
Expected behavior
The later instance should return false when using
isBefore
.Information
The text was updated successfully, but these errors were encountered: