convert gmt to samoa time in typescript

To convert a GMT date to Samoa Time in Typescript, we can use the date-fns library which provides a simple and easy-to-use API for manipulating dates and times.

First, we need to install date-fns using npm:

index.ts
npm install date-fns
21 chars
2 lines

Then, we can create a new Date object with our GMT time and use the add function to add the Samoa Time offset:

index.ts
import { add } from 'date-fns';

// set the GMT time
const gmtDate = new Date('2021-07-13T12:00:00Z');

// add the Samoa Time offset (+13 hours)
const samoaDate = add(gmtDate, { hours: 13 });

console.log(samoaDate.toISOString()); // output: "2021-07-14T01:00:00.000Z"
269 chars
10 lines

In this example, we use the add function to add 13 hours to the GMT date which corresponds to the Samoa Time offset.

Note that we use the toISOString function to convert the Samoa Time date to a string representation in ISO 8601 format.

gistlibby LogSnag