convert gmt to australian eastern time in typescript

To convert GMT to Australian Eastern Time (AET) in TypeScript, you can use Moment.js, a popular JavaScript library for handling dates and times.

Assuming you have a date string in GMT format, you can convert it to AET as follows:

index.ts
import moment from 'moment-timezone';

const gmtDateStr = '2022-01-01 12:00:00 GMT';
const aetDate = moment.tz(gmtDateStr, 'GMT').tz('Australia/Sydney');
const aetDateStr = aetDate.format('YYYY-MM-DD HH:mm:ss z');
console.log(aetDateStr); // Output: "2022-01-01 23:00:00 AEDT"
277 chars
7 lines

In the above code:

  • We import moment-timezone library to use its API for handling timezones.
  • We define a gmtDateStr variable that holds the date string in GMT format.
  • We create a moment object from the gmtDateStr using the moment.tz function that sets the timezone to GMT.
  • We then convert the moment object to AET timezone by calling the tz function again with the timezone string 'Australia/Sydney'.
  • Finally, we format the AET date as a string using the format function and a format string YYYY-MM-DD HH:mm:ss z, which shows the year, month, day, hour, minute, second, and timezone abbreviation.

Note that the above code assumes that you have the Moment.js library installed in your project. You can install it using npm install moment-timezone command.

gistlibby LogSnag