convert gmt to australian central time in typescript

To convert GMT to Australian Central Time in TypeScript, you can use the date-fns and timezone libraries.

Here is an example code snippet that demonstrates how to convert a GMT string to Australian Central Time:

index.ts

import { zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';

const gmtDate = '2021-10-13T02:30:00Z';
const targetTimeZone = 'Australia/Adelaide';

const utcDate = new Date(gmtDate);
const timeZoneDate = utcToZonedTime(utcDate, targetTimeZone);

console.log(`The converted date is: ${timeZoneDate}`);

304 chars
12 lines

In the code above, we first import the required functions from the date-fns-tz library. We then define the GMT date string and the target time zone which is Australian Central Time in this case.

We then create a Date object from the GMT date string and pass it to the utcToZonedTime function along with the target time zone. This function returns a Date object adjusted to the target time zone.

Finally, we log the converted date to the console.

Another option to achieve this conversion is using the Moment.js library. Here is an example code snippet that demonstrates how to convert GMT to Australian Central Time using Moment.js:

index.ts

import moment from 'moment-timezone';

const gmtDate = '2021-10-13T02:30:00Z';
const targetTimeZone = 'Australia/Adelaide';

const timeZoneDate = moment(gmtDate).tz(targetTimeZone);

console.log(`The converted date is: ${timeZoneDate}`);

240 chars
11 lines

In this code, we first import the Moment.js library. We then define the GMT date string and the target time zone which is Australian Central Time in this case.

We create a moment object from the GMT date string and use the tz function to convert it to the target time zone.

Finally, we log the converted date to the console.

gistlibby LogSnag