convert gmt to australian eastern standard time in typescript

To convert GMT to Australian Eastern Standard Time in TypeScript, we can use the toLocaleString() method on the Date object to display the date and time according to the Australian Eastern Standard Time zone.

index.ts
// Current date and time in GMT
const currentDateTime = new Date();

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

console.log(aestDateTime);  // Output: dd/mm/yyyy, hh:mm:ss am/pm
279 chars
8 lines

Here, we create a new Date object to get the current date and time in GMT. We then use the toLocaleString() method on this object to convert it to Australian Eastern Standard Time.

The toLocaleString() method takes two parameters: the language code and an options object. In the options object, we specify the timeZone property to 'Australia/Sydney' to convert to the Australian Eastern Standard Time zone.

Finally, we log the converted date and time to the console in the format dd/mm/yyyy, hh:mm:ss am/pm, where dd represents the day, mm represents the month, yyyy represents the year, hh represents the hour, mm represents the minute, and am/pm represents the meridian.

gistlibby LogSnag