How to convert a datetime from one timezone to another using dayjs

Viewed 638

I'm trying to convert a datetime from the timezone UTC+1 to the timezone where the user is located in. I get the datetime from the DB which is located in UTC+1 but the datetime should be shown to the user in his timezone.

For example:

08 Nov 2020 15:00 (UTC+1) should be shown to a user in San Francisco as 08 Nov 2020 06:00 (UTC-8)

I tried it using dayjs like this:

let dayjs = require('dayjs');
let utc = require('dayjs/plugin/utc'); // dependent on utc plugin
let timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc)
dayjs.extend(timezone)
let datetime = dayjs('2020-11-08 15:00').tz(dayjs.tz.guess()).format('ddd, DD MMM YYYY HH:mm');
console.log(datetime);

I started testing it by changing the timezone in the chrome devtools but I always keep getting Sun, 08 Nov 2020 15:00 no matter which timezone I'm currently in.

Can someone tell me what I'm doing wrong? I'm using dayjs v1.9.5

0 Answers
Related