user13000875
user13000875

Reputation: 495

Convert date from one timezone to another in momentjs

I have one datetime e.g. 22 Nov 2020, 04:59 in (GMT+10:00) Australia/ACT (Eastern Standard Time (New South Wales)) now I want to get equivalent date for this time in (GMT+3:00) Asia/Kuwait (Arabia Standard Time). I am using moment js . Please suggest

Upvotes: 1

Views: 1556

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30725

We can parse your ACT time using the moment.tz constructor, we can then clone and convert to the Kuwait timezone.

const actTime = moment.tz("22 Nov 2020, 04:59", "DD MMM YYYY, HH:mm", "Australia/ACT");
const kuwaitTime = actTime.clone().tz("Asia/Kuwait");

console.log("Moment.js:");
console.log("ACT Time:", actTime.format("YYYY-MM-DD HH:mm"));
console.log("Kuwait Time:", kuwaitTime.format("YYYY-MM-DD HH:mm"));
<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.25/moment-timezone-with-data-10-year-range.js"></script>

We could also do the same thing using the rather awesome new Luxon module (Luxon can be thought of as the evolution of Moment. It is authored by Isaac Cambron, a long-time contributor to Moment.)

const { DateTime } = luxon;

const actTime = DateTime.fromFormat("22 Nov 2020, 04:59", "dd MMM yyyy, HH:mm", { zone: "Australia/ACT"});
const kuwaitTime = actTime.setZone("Asia/Kuwait")

console.log("Luxon:");
console.log("ACT Time:", actTime.toFormat("yyyy-MM-dd HH:mm"));
console.log("Kuwait Time:", kuwaitTime.toFormat("yyyy-MM-dd HH:mm"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js" integrity="sha512-OyrI249ZRX2hY/1CAD+edQR90flhuXqYqjNYFJAiflsKsMxpUYg5kbDDAVA8Vp0HMlPG/aAl1tFASi1h4eRoQw==" crossorigin="anonymous"></script>

Upvotes: 1

Related Questions