How to convert Moment to another moment depending on timezone
You can use moment local()
to convert moment object to user local time, while you can use tz()
to convert time between zones.
Use moment.utc
to parse date as UTC. Use format()
to show values of moment objects.
If you just need to support UTC and user local time, you don't need moment-timezone.
Here a working sample:
var dt = document.getElementById("utc-time").innerHTML;
var m = moment.utc(dt, "DD-MM-YYYY h:mm:ss A"); // parse input as UTC
console.log(m.clone().local().format("DD-MM-YYYY h:mm:ss A")); // local time
var tz = 'America/New_York'; // example value, you can use moment.tz.guess()
console.log(m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A")); // 30-03-2017 2:34:22 AM
tz = 'Australia/Perth';
console.log(m.clone().tz(tz).format("DD-MM-YYYY h:mm:ss A")); // 30-03-2017 2:34:22 PM
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>
<div id="utc-time">30/03/2017 6:34:22 AM</div>
If you want to convert date to another timezone in moment js ,use utcOffset
and provide the offset just like below
moment(new Date()).utc().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss");