convert gmt to mountain time in typescript

To convert a GMT timestamp to Mountain Time in TypeScript, we can use the moment-timezone library.

First, we need to install the moment-timezone library using NPM:

npm install moment-timezone
28 chars
2 lines

Then, we can use the following code snippet to convert the GMT timestamp to Mountain Time:

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

const gmtTime = moment.utc('2022-05-01 12:00:00');
const mountainTime = gmtTime.tz('America/Denver').format('YYYY-MM-DD HH:mm:ss');
console.log(mountainTime); // Output: 2022-05-01 06:00:00
234 chars
6 lines

In the code above, we first create a moment object using the moment.utc() method and pass the GMT timestamp as a parameter. This creates a moment object with the UTC time zone.

We then use the tz() method to convert the UTC time zone to the Mountain Time zone. We pass the America/Denver time zone identifier as a parameter to the tz() method.

Finally, we use the format() method to format the output in the desired format ('YYYY-MM-DD HH:mm:ss') and log it to the console.

The output will be in the Mountain Time zone, which is 6 hours behind the GMT time zone.

gistlibby LogSnag