convert australian western daylight time to gmt in javascript

To convert Australian Western Daylight Time to GMT in JavaScript, you can use the built-in toLocaleString() method with the timeZone option to set the time zone of the initial date to AWDT, then convert it to the desired output format with the toLocaleString() method again using the timeZone option set to GMT. Here's an example code snippet:

index.tsx
// set initial date in AWDT timezone
const awdtDate = new Date('2021-05-01T12:00:00-08:00');

// convert to GMT timezone
const gmtDate = new Date(awdtDate.toLocaleString('en-US', { timeZone: 'Australia/Perth' })).toLocaleString('en-US', { timeZone: 'GMT' });

console.log(gmtDate); // output: "5/1/2021, 4:00:00 AM"
316 chars
8 lines

In this example, we first create a new date object representing the date and time in AWDT. We then use the toLocaleString() method with the timeZone option set to 'Australia/Perth' to format the date and time string in the AWDT time zone. We then pass this formatted date string to a new Date object constructor to create a new date object representing the same date and time in the GMT time zone. Finally, we use the toLocaleString() method again to format the date and time string in the GMT time zone, and log it to the console.

gistlibby LogSnag