Momentjs: How to convert date/time of one timezone to UTC date/time

Shivprsad Sammbhare picture Shivprsad Sammbhare · Apr 10, 2019 · Viewed 9.2k times · Source

I have a date/time with a timezone and want to convert it into UTC

const date = '2019-04-10T20:30:00Z';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment(date).tz(zone).utc().format();
console.log('UTC Date : ', utcDate);

is my date variable is in standard formate for UTC? How to cast this time zone to another time zone?

Answer

Fraction picture Fraction · Apr 10, 2019

The UTC timezone is denoted by the suffix "Z" so you need to remove "Z" and use moment.tz(..., String) instead of moment().tz(String) because the first create a moment with a time zone and the second is used to change the time zone on an existing moment:

const date = '2019-04-10T20:30:00';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment.tz(date, zone).utc().format();
console.log('UTC Date : ', utcDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>