convert australian eastern time to gmt in javascript

One way to convert Australian Eastern Time (AET) to Greenwich Mean Time (GMT) in JavaScript is to use the Moment.js library. Here's an example code snippet:

index.tsx
// Assuming you have Moment.js library installed and included in your project

// Define the date and time in AET
const aetDateTimeString = '2021-08-31T14:30:00+10:00';
const aetDateTime = moment(aetDateTimeString);

// Convert to GMT
const gmtDateTime = aetDateTime.clone().utc();

// Get the GMT date and time in a specific format
const gmtDateTimeString = gmtDateTime.format('YYYY-MM-DD HH:mm:ss [GMT]');

console.log(gmtDateTimeString);
441 chars
14 lines

In this example, we first define a date and time string in AET ('2021-08-31T14:30:00+10:00'). We then create a Moment.js object and parse this string using the moment function.

Next, we use the clone method to create a new Moment object that is a clone of our AET object. We then call the utc method to convert this clone to GMT.

Finally, we use the format method to format the GMT date and time string in a specific format ('YYYY-MM-DD HH:mm:ss [GMT]'). We then output this string to the console.

Note that Moment.js uses the IANA time zone database to handle time zones correctly. This means that you may need to adjust your AET date and time string to include the correct time zone offset (in this example, we included the +10:00 offset to indicate AET).

gistlibby LogSnag