convert gmt to australian eastern daylight time in javascript

One way to convert GMT to Australian Eastern Daylight Time in Javascript is to use the Moment.js library to handle the date and timezone conversions. Here is a code snippet that demonstrates the conversion:

index.tsx
// Set the GMT date/time
const gmtDate = moment('2021-04-19T07:00:00Z');

// Convert to AEDT timezone
const aedtDate = gmtDate.tz('Australia/Sydney');

// Display the AEDT date/time
console.log(aedtDate.format('YYYY-MM-DDTHH:mm:ssZ'))
235 chars
9 lines

In the code above, we start by setting the GMT date/time using the moment function and passing in the date string in ISO 8601 format with the Z indicating that it is in GMT.

We then use the tz function to convert the date to the Australian Eastern Daylight Time timezone, using the Australia/Sydney timezone identifier.

Finally, we output the AEDT date/time in ISO 8601 format using the format function and passing in the desired format string as an argument.

Note: To use Moment.js, you will need to download the library and include it in your project. You can do this by downloading the library from the Moment.js website or by using a package manager like NPM or Yarn.

gistlibby LogSnag