dayjs
dayjs copied to clipboard
isSame with itself is false during DST
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"
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/
I'm having the same issue, any updates?
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.
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.