convert central time to gmt in typescript

To convert a date and time from Central Time to GMT in TypeScript, you can use the Moment.js library. Here's an example code snippet that demonstrates how to do this:

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

const centralTime = moment.tz('2021-04-30 14:30:00', 'America/Chicago');
const gmtTime = centralTime.clone().tz('Etc/GMT');

console.log(centralTime.format()); // output: 2021-04-30T14:30:00-05:00
console.log(gmtTime.format()); // output: 2021-04-30T19:30:00+00:00
304 chars
8 lines

In this example, we first create a moment object representing the date and time in Central Timezone by specifying the date string and the timezone America/Chicago. We then create a new moment object by cloning the original object and converting it to the GMT timezone using the timezone string Etc/GMT.

Finally, we can use the format() method to display the date and time in the desired format. The output indicates that the original Central Time is 5 hours behind GMT.

gistlibby LogSnag