dayjs
dayjs copied to clipboard
Comparison functions (e.g. isAfter()) not working with timezone dates
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
Found this out in the worst way hahaha. Can confirm
Confirm - same issue
Working with timezones with this library is quite buggy, so I decided to switch back to moment