Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format a string in UTC to the configured timezone on Moment

I have the following string, which is in UTC:

2022-02-01T00:00:00Z

I have already configured my timezone, so I do not want to mess/call .tz()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know that this string is in UTC, but I am not managing to convert from UTC to the defined timezone, which in this example is pacific/wallis.

I have tried many things, as

const utc = moment.utc('2022-02-01T00:00:00Z').toDate()
const inConfiguredTimeZone = utc.format()

My desire is to get this timestamp 2022-02-01T00:00:00Z and have converted to the defined timezone on Moment

I need to tell moment that "This string is in UTC, please give me the converted timestamp in the defined time zone"

>Solution :

If you just want to format a UTC timestamp in your current timezone (determined by your computer’s time settings) just use

let s = moment("2022-02-01T00:00:00Z").format();

This will produce a string like 2022-02-01T12:00:00+12:00 if you are currently in a timezone that has a UTC offset of +12 hours (like pacific/wallis) or 2022-02-01T01:00:00+01:00 if you are currently in a timezone that has a UTC offset of +1 hours (like europe/berlin)

If you want it converted to a specific timezone use

let s = moment("2022-02-01T00:00:00Z").tz("pacific/wallis").format();

This will produce 2022-02-01T12:00:00+12:00, regardless of your current timezone.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading