convert gmt to chatham time in typescript

To convert GMT to Chatham Time in Typescript, we can use the Moment.js library along with the timezone feature. Here's an example code snippet that demonstrates the conversion:

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

const gmtTime = moment.utc('2021-10-08 12:00'); // set the GMT Time
const chathamTime = gmtTime.tz('Pacific/Chatham').format(); // convert to Chatham Time

console.log(`GMT Time: ${gmtTime.format()}`); // 2021-10-08T12:00:00Z
console.log(`Chatham Time: ${chathamTime}`); // 2021-10-09T01:45:00+13:45
344 chars
8 lines

In this code, we import the Moment.js library and create a Moment object for the GMT time by specifying the UTC timezone using moment.utc(). We then use the tz() function to convert the GMT time to the Pacific/Chatham timezone, and format the resulting Chatham Time using the format() function.

The output of this code will show the GMT time, the Chatham time and its offset from GMT.

Note: Make sure to install Moment.js and Moment-Timezone packages before using them in your Typescript project.

gistlibby LogSnag