convert samoa time to gmt in typescript

To convert Samoa time to GMT in TypeScript, we can use the Moment.js library which provides functionality to work with dates and timezones.

First, we need to install Moment.js library in our project using package manager like npm or yarn.

index.ts
npm install moment-timezone --save 
36 chars
2 lines

Then we can use the moment() function to create a moment object representing Samoa time, specifying the Pacific/Samoa timezone:

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

const samoaTime = moment.tz('2022-01-01 00:00', 'Pacific/Samoa');
110 chars
4 lines

Next, we can use the utc() method with no arguments to convert the moment object from Samoa time to UTC/GMT:

index.ts
const gmtTime = samoaTime.utc();
33 chars
2 lines

Finally, we can format the moment object as a string in the desired format using the format() method:

index.ts
const formattedTime = gmtTime.format('YYYY-MM-DD HH:mm:ss');
61 chars
2 lines

Putting it all together, here's the TypeScript code to convert Samoa time to GMT:

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

const samoaTime = moment.tz('2022-01-01 00:00', 'Pacific/Samoa');
const gmtTime = samoaTime.utc();
const formattedTime = gmtTime.format('YYYY-MM-DD HH:mm:ss');

console.log(formattedTime); // Output: 2021-12-31 11:00:00
264 chars
8 lines

gistlibby LogSnag