convert gmt to australian eastern standard time in javascript

You can use the Moment.js library to easily handle and convert timezones in JavaScript. Here's an example of how to convert a GMT date string to Australian Eastern Standard Time (AEST):

index.tsx
// Install moment-timezone via npm
// npm install moment-timezone --save

// Load moment-timezone
const moment = require('moment-timezone');
 
// GMT date string
const gmtDate = '2022-08-17T09:30:00Z';

// Convert to AEST
const aestDate = moment(gmtDate).tz('Australia/Sydney').format();

console.log(aestDate); // Output: 2022-08-17T19:30:00+10:00
349 chars
14 lines

In the code above, we first load the Moment.js library and then define a GMT date string as gmtDate. We then use the moment() function to parse the GMT date and convert it to the desired timezone using the tz() function, which takes the timezone identifier as a parameter. In this case, we use Australia/Sydney to convert to AEST.

To output the result, we use the format() function to return the date in ISO 8601 format with timezone offset.

Note that we need to install the moment-timezone package via npm, which is a Moment.js plugin that provides zone data for timezones. You can find a list of timezone identifiers to use with the tz() function on the Moment.js website.

gistlibby LogSnag