convert australian eastern daylight time to gmt in javascript

To convert Australian Eastern Daylight Time (AEDT) to Greenwich Mean Time (GMT) using JavaScript, you can use the built-in Date object and its associated methods.

First, you need to create a Date object with the AEDT time you want to convert. This can be done using the new Date() constructor and specifying the year, month, day, hour, minute, and second in the local AEDT timezone.

index.tsx
// create a date object with the local AEDT time
let aedtDate = new Date(2021, 8, 1, 14, 30, 0); // September 1st, 2021 2:30:00 PM AEDT
136 chars
3 lines

Next, you can use the getTimezoneOffset() method to get the difference in minutes between the local timezone (AEDT) and UTC/GMT.

index.tsx
// get the difference in minutes between AEDT and GMT
let offsetMinutes = aedtDate.getTimezoneOffset(); // returns -660 for AEDT (11 hours ahead of GMT)
153 chars
3 lines

Finally, you can use the setMinutes() method to subtract the offset from the Date object's minutes value, effectively converting it to GMT.

index.tsx
// convert the AEDT date to GMT by subtracting the offset in minutes
aedtDate.setMinutes(aedtDate.getMinutes() - offsetMinutes); // September 1st, 2021 3:30:00 AM GMT
167 chars
3 lines

Now aedtDate contains the equivalent GMT date and time. Note that this conversion assumes that AEDT is 11 hours ahead of GMT and does not account for any Daylight Saving Time (DST) changes.

gistlibby LogSnag