convert australian central time to gmt in javascript

To convert Australian Central Time (ACST) to GMT (Greenwich Mean Time), you can use the Moment.js library in JavaScript.

Assuming you have a date and time in ACST timezone, you can create a Moment.js object and then use the .utc() method to convert it to UTC timezone, which is equivalent to GMT.

Here's an example code snippet:

index.tsx
// date and time in ACST
const acstDateTime = '2021-06-01 12:00:00';

// create Moment.js object with ACST datetime and timezone
const acstMoment = moment.tz(acstDateTime, 'Australia/Adelaide');

// convert to UTC/GMT timezone
const gmtMoment = acstMoment.utc();

// get the GMT datetime string
const gmtDateTime = gmtMoment.format('YYYY-MM-DD HH:mm:ss');
console.log(gmtDateTime); // output: 2021-06-01 02:30:00
413 chars
13 lines

In this example, we first define the datetime string in ACST timezone. Then we create a Moment.js object with the moment.tz() function, specifying the datetime string and 'Australia/Adelaide' timezone. This creates a Moment.js object with the ACST datetime.

We then use the .utc() method on the Moment.js object, which converts it to the equivalent UTC datetime. We then get the GMT datetime string using the .format() method with the 'YYYY-MM-DD HH:mm:ss' format.

Note that this solution requires Moment.js and its timezone plugin. You can install them using npm:

npm install moment moment-timezone
35 chars
2 lines

and then import them in your JavaScript file:

index.tsx
const moment = require('moment');
require('moment-timezone');
62 chars
3 lines

gistlibby LogSnag