dayjs
dayjs copied to clipboard
DayJS giving wrong output on GMT
I am trying to convert different times from different zones into GMT. This will allow me to send them notifications according to their time zone. I noticed it worked perfectly for other time zones, but when I passed GMT or UTC, it gave the wrong time.
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
dayjs.extend(timezone);
let zone = "UTC";
let hrs = 10;
let mins = 0;
const zoneTimeInGMT =
dayjs()
.tz(zone)
.hour(hrs)
.minute(mins)
.utc()
.format("dddd MMMM DD YYYY HH:mm:ss") + " GMT";
console.log({ zoneTimeInGMT });
For other timezones
let zone = "America/New_York";
let hrs = 10;
let mins = 0;
const zoneTimeInGMT =
dayjs()
.tz(zone)
.hour(hrs)
.minute(mins)
.utc()
.format("dddd MMMM DD YYYY HH:mm:ss") + " GMT";
console.log({ zoneTimeInGMT });
Hi, it seems you have incorrect order of function call. When you calling utc() at the end you're getting utc time in your local time zone. Here is a correct demo