convert gmt to central time in javascript

To convert GMT to Central Time in JavaScript, you can create a new Date object with the timestamp in GMT and then use the toLocaleString() method to convert it to the Central Timezone. Here's an example code snippet:

index.tsx
// create a new date object with timestamp in GMT
var dateInGMT = new Date('August 7, 2021 12:00:00 GMT');

// convert to Central Timezone
var dateInCT = dateInGMT.toLocaleString('en-US', {timeZone: 'America/Chicago'});

console.log(dateInCT); // output: "8/7/2021, 7:00:00 AM"
278 chars
8 lines

In the example above, we created a new Date object that represents August 7, 2021 at 12:00:00 GMT. Then, we used the toLocaleString() method and passed in the options {timeZone: 'America/Chicago'} to convert it to the Central Timezone. Finally, we logged the date and time in Central Time to the console.

gistlibby LogSnag