dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

DayJS giving wrong output on GMT

Open VaibhavPaliwal0007 opened this issue 3 years ago • 1 comments

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 });
image

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 });
image

VaibhavPaliwal0007 avatar Aug 25 '22 11:08 VaibhavPaliwal0007

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

Bykiev avatar Aug 31 '22 14:08 Bykiev