convert gmt to australian central daylight time in javascript

To convert a GMT date to Australian Central Daylight Time (ACDT) in JavaScript, you can use the Moment.js library, which makes time zone conversion simple.

Here's an example code snippet that demonstrates how to do this conversion:

index.tsx
// GMT date to convert
const gmtDate = '2022-01-01T12:00:00Z';

// Convert GMT to ACDT using Moment.js
const acdtDate = moment.utc(gmtDate).tz('Australia/Adelaide').format();

// Output the ACDT date
console.log(acdtDate); // Output: 2022-01-01T22:30:00+10:30
260 chars
9 lines

In the code above, we first define the GMT date we want to convert as a string ('2022-01-01T12:00:00Z').

We then use the Moment.js library to create a moment object from the GMT date by calling the moment.utc function and passing in the GMT date string. This creates a moment object in the UTC time zone.

Finally, we use the tz function to convert the UTC datetime to the ACDT timezone by passing in 'Australia/Adelaide' as the desired timezone. We then call the format function to format the resulting moment object as a string in the ISO format.

The output of running this code should be the ACDT date in ISO format, which should be '2022-01-01T22:30:00+10:30' assuming the ACDT is 10.5 hours ahead of GMT.

gistlibby LogSnag