dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

isSame with itself is false during DST

Open faberchri opened this issue 3 years ago • 4 comments

Describe the bug Comparing a date with itself (d.isSame(d)) returns false if the date is during daylight saving time.

// Running node with TZ='Europe/Zurich'

d = dayjs.tz("2021-10-01 00:00:00", "Europe/Athens")
d.isSame(d) // returns false

A test project to reproduce the bug can be found at https://github.com/faberchri/dayjs_bugs

The bug does not occur if node runs with TZ='UTC'.

Expected behavior Comparing a date to itself with isSame should always return true.

Information

  • Day.js Version 1.10.7
  • OS: macOS 11.6.1
  • node v16.13.0
  • Time zone: "Europe/Zurich"

faberchri avatar Dec 04 '21 15:12 faberchri

I believe I have run into this bug also. The following code demonstrates that conversion from a date to UTC and back does not produce the original date when the date is during daylight saving time:

https://jsfiddle.net/p3cen2bf/3/

serac avatar Jan 28 '22 11:01 serac

I'm having the same issue, any updates?

Samaa-Amin avatar Aug 31 '22 18:08 Samaa-Amin

The issue still persists, this is because isSame is using this comparison this.startOf(units) <= other && other <= this.endOf(units)

If the day is within DST, this magically fails: other <= this.endOf(units), due to the 1 hour different in timezone.

Actually the .startOf and .endOf has correctly addressed the DST timezone, however, the const other = dayjs(that) has failed to correctly addressed the time zone.

zuyetawarmatik avatar Nov 23 '23 03:11 zuyetawarmatik

The fix should be at the isSame, here is a rough illustration:

isSame(that, units) {
  // const other = dayjs(that); // original code

  // This will fix the DST timezone, without shifting hour value.
  const other = dayjs(that);
  const otherFixed = other.$x.$timezone ? other.tz(other.$x.$timezone, true) : other;
  return this.startOf(units) <= otherFixed && otherFixed <= this.endOf(units)
 }

The other 2 functions, isAfter and isBefore, should also have the same fix.

zuyetawarmatik avatar Nov 23 '23 12:11 zuyetawarmatik