Tony_Henrich
Tony_Henrich

Reputation: 44225

How to convert a datetime to another timezone datetime then to utc using moment.js?

How to convert a datetime to another timezone datetime then to utc using moment.js? This has to be done without using the timezone library so functions like tz and setTimezone are not available. The datetime is entered by the user.

So '5/24/2019 20:35' in Hawaii time is expected to be 5/25/2019 06:35AM in utc. My local computer is Pacific.

I tried this:

moment.utc(moment('5/24/2019 20:35','M/D/YYYY h:mm').utcOffset(-10).format("MM/DD/YYYY HH:mm:ss")).format('MM/DD/YYYY HH:mm:ss');

but it's not correct.

Upvotes: 1

Views: 64

Answers (1)

suv
suv

Reputation: 1553

Why do you need to convert to another timezone before converting to UTC?

Just do new Date("5/24/2019 20:35 -10").toUTCString() in pure JS.

On latest chrome it gives me the correct output "Sat, 25 May 2019 06:35:00 GMT"

If you need ISO format, do new Date(datestring).toISOString() //"2019-05-25T06:35:00.000Z"

Upvotes: 1

Related Questions