dayjs is not converting timezones properly

Viewed 10417

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?

5 Answers

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)

try this:

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

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

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?

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.

Related