'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 1:[1]

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(utcDate).tz(timeZone)

Solution 2:[2]

try this:

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

Solution 3:[3]

I've fixed it by adding .local() after dayjs("2020-09-21 20:30").tz("Asia/Taipei").local()

Solution 4:[4]

I'm using latest version of dayjs - 1.11.1 and react native v0.66.4. When I write:

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

I get null as result. Does anybody have that problem?

Solution 5:[5]

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import tz from 'dayjs/plugin/timezone'

dayjs.extend(utc)
dayjs.extend(tz)    

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

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

.toString() and .toISOString() seem to always print the original date, before the timezone conversion, which may cause some confusion. toDate().toLocaleString() works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 donquixote
Solution 2 Nbody
Solution 3 João Pedro
Solution 4 olga_babic
Solution 5