convert gmt to australian central daylight time in typescript

To convert from GMT to Australian Central Daylight Time (ACDT), we can use the toLocaleString() method in JavaScript. However, we need to first set the timezone for our date object to the GMT timezone. We can achieve this by creating a new Date object with our GMT time and then setting its timezone to UTC (Coordinated Universal Time, which is the equivalent of GMT).

Here's the code:

index.ts
const gmtTime = new Date('2022-01-17T03:30:00.000Z');
const acdtTime = gmtTime.toLocaleString("en-AU", {timeZone: "Australia/Adelaide"});

console.log(acdtTime); // Output: "17/01/2022, 2:00:00 pm"
198 chars
5 lines

In this example, we have created a new Date object with a GMT time of 2022-01-17T03:30:00.000Z. We then use the toLocaleString() method to convert this to the ACDT timezone by passing the locale as "en-AU" and the timezone as "Australia/Adelaide". This will return the ACDT time in the format dd/mm/yyyy, h:mm:ss am/pm.

gistlibby LogSnag