convert gmt to hawaii time in typescript

To convert a GMT datetime to Hawaii time in Typescript, we can use the Moment.js library along with the Moment Timezone plugin.

First, install the necessary dependencies:

npm install moment moment-timezone
35 chars
2 lines

Then, we can use the moment function to create a new Moment object with the GMT datetime, and use the tz method to convert to Hawaii time:

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

const gmtDatetime = '2022-03-15 12:00:00 GMT';
const hawaiiDatetime = moment(gmtDatetime).tz('Pacific/Honolulu').format('YYYY-MM-DD HH:mm:ss z');
console.log(hawaiiDatetime); // Output: 2022-03-15 02:00:00 HST
266 chars
7 lines

In the code above, we create a Moment object from the GMT datetime using the moment function. Then, we use the tz method to convert to Hawaii time, specifying the 'Pacific/Honolulu' timezone. Finally, we format the resulting datetime string using the format method, with the 'YYYY-MM-DD HH:mm:ss z' format string.

The resulting hawaiiDatetime variable will contain the Hawaii datetime converted from the original GMT datetime.

gistlibby LogSnag