kkkkkkkkkkkkkkkkk
kkkkkkkkkkkkkkkkk

Reputation: 728

How to prevent date conversion to local time zone with DayJS

I have a date string const someDate = 2023-02-13T09:00:00.000-05:00

The problem is when I'm formatting it via DayJS.

dayjs(someDate).format('h:mm A')

It returns me string according to my local time zone, when I need to keep like I received.

Any way to disable converting time to local timezone in DayJS?

Upvotes: 11

Views: 26735

Answers (4)

I use it this way, first I transform to UTC timezone dayjs.tz.setDefault("Etc/GMT"), then transform transformed datetime to string dayjs.tz(datetime).toISOString() and the resulting value automatic transform to local timezone dayjs(utcDatetime).

import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault("Etc/GMT");


function formatDate(datetime) {
  const utcDatetime = dayjs.tz(datetime).toISOString();

  return dayjs(utcDatetime).format("DD.MM.YYYY HH:mm");
}

export default formatDate;

Upvotes: 1

Jonathan Tuzman
Jonathan Tuzman

Reputation: 13262

I believe that the simplest way is to actually supply both an "input" and "output" TZ:

const twoThirtyThreeNoMatterWhat = dayjs('02-05-21 02:33 PM GMT').tz('GMT').format(FMT),

This is essentially the same as @Filip Huhta but doesn't require parsing the offset -- although you could of course hardcode the offset so that his answer becomes

const formattedDate = dayjs(someDate).utcOffset('-5:00').format('h:mm A');

Which is certainly fine, although it's a few more characters than mine.

The important thing, though, is that because they are both the same, they don't matter as long as they are both the same.

Of course, this only works if you really have control of the input. If, say, you're calling dayjs with some text you've parsed, then you can call that function with whatever exact string you want. And I'd assert that that is usually the case.

I also suppose my strategy is only valid if you in fact do not care about the time zone. Since your desired format is h:mm A then I imagine you simply want it to say 9:00 AM and that's all you care about. (I'd also assert that this is usually the case 😊)

Upvotes: 0

Emeka Nwachukwu
Emeka Nwachukwu

Reputation: 97

You can use the utc plugin.

import dayjs from "dayjs";
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc)


const dateTime = "2023-02-13T09:00:00.000-05:00";
const value = dayjs.utc(dateTime).format('h:mm A');

console.log(value);

Upvotes: 5

Filip Huhta
Filip Huhta

Reputation: 2368

Yes!

You can disable converting to local timezone in dayjs by passing in the original timezone as an additional argument in the dayjs constructor.

Example:

const someDate = "2023-02-13T09:00:00.000-05:00";
const originalTimezone = someDate.slice(-6);
const formattedDate = dayjs(someDate).utcOffset(originalTimezone).format('h:mm A');

The utcOffset() method allows you to set the offset in minutes from UTC for a specific date instance. The originalTimezone constant is used to extract the timezone offset (-05:00) from the original date string someDate, and pass it to the utcOffset() method. This will ensure that the formatted date stays in the original timezone.

Upvotes: 20

Related Questions