dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Comparison functions (e.g. isAfter()) not working with timezone dates

Open csuermann opened this issue 3 years ago • 2 comments

Date comparison functions like isAfter() do not work with dayjs.tz dates.

Example

const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz.setDefault('Europe/Berlin')

console.log('working as expected:')

let before = dayjs('2021-01-03T20:00:00+01:00')
let after = dayjs('2021-01-03T20:30:00+01:00') //30 minutes later

console.log('before:', before.format()) // => 2021-01-03T20:00:00+01:00
console.log('after:', after.format())   // => 2021-01-03T20:30:00+01:00
console.log('after.isAfter(before) ==>', after.isAfter(before)) // => true ✅
console.log('before.isAfter(after) ==>', before.isAfter(after)) // => false ✅

console.log('\n')

console.log('NOT working as expected:')

before = dayjs.tz('01.03.2021 21:00:00', 'DD.MM.YYYY HH:mm', 'Europe/Berlin')
after = dayjs.tz('01.03.2021 21:30:00', 'DD.MM.YYYY HH:mm', 'Europe/Berlin') //30 minutes later

console.log('before:', before.format()) // => 2021-01-03T20:00:00+01:00
console.log('after:', after.format())   // => 2021-01-03T20:30:00+01:00

console.log('after.isAfter(before) ==>', after.isAfter(before)) // => false ❌
console.log('before.isAfter(after) ==>', before.isAfter(after)) // => false ✅

Information

  • Day.js Version: v1.8.34
  • OS: MacOS 11.2.3
  • Node.js
  • Time zone: CET

csuermann avatar Apr 14 '21 19:04 csuermann

Found this out in the worst way hahaha. Can confirm

githubdoramon avatar Jan 13 '22 10:01 githubdoramon

Confirm - same issue

bundit avatar Jul 05 '22 22:07 bundit

Same issue. solution

NOTE: not tested

richxcame avatar Jan 18 '23 05:01 richxcame

Working with timezones with this library is quite buggy, so I decided to switch back to moment

zim32 avatar Mar 23 '23 12:03 zim32