convert gmt to australian western time in javascript

To convert a GMT (Greenwich Mean Time) time to Australian Western Standard Time (AWST) in JavaScript, you can use the toLocaleString() method on a Date object and specify the time zone as "Australia/Perth".

Here's an example code snippet:

index.tsx
// create a new Date object with the GMT time
const gmtTime = new Date('July 17, 2022 03:24:00 GMT');

// convert to AWST by setting the time zone to "Australia/Perth"
const awstTime = gmtTime.toLocaleString('en-AU', { timeZone: 'Australia/Perth' });

console.log('GMT time:', gmtTime.toLocaleString());
console.log('AWST time:', awstTime);
341 chars
9 lines

This will output:

index.tsx
GMT time: 7/17/2022, 3:24:00 AM
AWST time: 7/17/2022, 11:24:00 AM
66 chars
3 lines

Note that the toLocaleString() method returns a string representation of the date and time in the specified time zone. If you need to perform additional calculations or formatting on the date and time, you can use other methods provided by the Date object.

gistlibby LogSnag