Search code examples
javascriptdayjs

dayjs is not converting timezones properly


I'm trying to convert a date from my local time (Taipei UTC+8) to Los Angeles ( UTC-7) however dayjs conversion seems to be completely off :

dayjs("2020-09-21 20:30").tz("Asia/Taipei")

this results in Tue Sep 22 2020 05:30:00 GMT-0400 (Eastern Daylight Time) but it should have been Mon Sep 21 2020 02:30:00 GMT-0400 (Eastern Daylight Time)

any idea what's going on?


Solution

  • I fixed using utc first and then format on local timezone

    import dayjs from 'dayjs'
    import utc from 'dayjs/plugin/utc'
    import tz from 'dayjs/plugin/timezone'
    
    dayjs.extend(utc)
    dayjs.extend(tz)
    
    const timeZone = dayjs.tz.guess()
    dayjs.utc(dateToConvert).tz(timeZone)