convert gmt to central time in typescript

To convert GMT to Central Time in TypeScript, you can use moment.js library. Here is an example code snippet:

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

const gmtTime = moment.utc('2022-01-01 00:00:00');
const centralTime = gmtTime.tz('America/Chicago');
console.log(centralTime.format('YYYY-MM-DD HH:mm:ss'));
197 chars
6 lines

Explanation:

  • First, we import moment library.
  • Then, we create a moment object with the GMT time. The format of the input string is 'YYYY-MM-DD HH:mm:ss'.
  • Next, we convert the GMT time to Central Time using the tz() function and passing the timezone identifier 'America/Chicago'.
  • Finally, we print the Central Time in the desired format using the format() function with the format string 'YYYY-MM-DD HH:mm:ss'.

Note: Don't forget to install moment-timezone library if it's not installed in your project. You can install it using npm install moment-timezone.

gistlibby LogSnag