convert chatham time to gmt in typescript

To convert Chatham time to GMT in Typescript, you can use the Moment.js library. Moment.js makes it easy to convert dates and times between different timezones.

Assuming you have a date object representing the Chatham time, you can use the following code to convert it to GMT:

index.ts
import * as moment from 'moment-timezone';

const chathamTime = moment.tz('2019-08-14 10:30:00', 'Pacific/Chatham');
const gmtTime = chathamTime.clone().tz('Etc/GMT');

console.log(`Chatham time: ${chathamTime.format()}`); // 2019-08-14T10:30:00+12:45
console.log(`GMT time: ${gmtTime.format()}`); // 2019-08-14T21:45:00Z
322 chars
8 lines

In this code, we first create a Moment.js object representing the Chatham time, using the moment.tz method. We pass in the date and time as a string in ISO 8601 format, and the timezone as a string representing the Chatham timezone.

Next, we create a clone of the Chatham time object, and use the tz method again to convert it to the GMT timezone. Note that we pass in the timezone as a string representing the GMT timezone (Etc/GMT).

Finally, we print out the Chatham and GMT times using the format method.

Hope this helps!

gistlibby LogSnag