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 behaviour of isBefore when using timezones #2182

Open
colq2 opened this issue Dec 30, 2022 · 2 comments
Open

Wrong behaviour of isBefore when using timezones #2182

colq2 opened this issue Dec 30, 2022 · 2 comments

Comments

@colq2
Copy link

colq2 commented Dec 30, 2022

Describe the bug
Comparing two dayjs instances in a specific timezone wich are 1 minute apart a before each other.

var d1 = dayjs().tz('Europe/Berlin').year(2023).month(3).date(10).hour(0).minute(0).second(0).millisecond(0)
var d2 = d1.subtract(1, 'minute')
d1.isBefore(d2)
> true
d2.isBefore(d1)
> true

This is not happening when you are not using a timezone:

var d1 = dayjs().year(2023).month(3).date(10).hour(0).minute(0).second(0).millisecond(0)
var d2 = d1.subtract(1, 'minute')
d1.isBefore(d2)
> false
d2.isBefore(d1)
> true

Expected behavior
The later instance should return false when using isBefore.

Information

  • Day.js Version v1.11.5
  • OS: macOS
  • Browser Chrome 108.0.5359.124
  • Time zone: GMT+0200
@colq2 colq2 changed the title Wrong behaviour of isBefore Wrong behaviour of isBefore when using timezones Dec 30, 2022
@addiebarron
Copy link

addiebarron commented Jan 3, 2023

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

@KronosDev-Pro
Copy link

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 😉

Tests

if 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
})

On dev branch:
image

On pr/2118 branch:
image

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

No branches or pull requests

3 participants