convert gmt to australian eastern time in javascript

In order to convert GMT to Australian Eastern Time in JavaScript, you can use the toLocaleString() method of the Date object. Here is an example:

index.tsx
// Create a Date object with GMT time
const gmtDate = new Date('2021-01-01T00:00:00Z');

// Convert GMT to Australian Eastern Time
const aetDate = gmtDate.toLocaleString('en-AU', {
  timeZone: 'Australia/Sydney'
});

console.log(aetDate); // Output: '1/1/2021, 11:00:00 AM'
274 chars
10 lines

In the code above, we create a Date object with a GMT time of January 1st, 2021 at midnight. We then use the toLocaleString() method with the en-AU locale and the Australia/Sydney timezone to convert the GMT time to Australian Eastern Time. The result is a string in the format of "1/1/2021, 11:00:00 AM".

gistlibby LogSnag