convert gmt to chatham time in javascript

To convert GMT to Chatham Time in JavaScript, you need to create a Date object with the GMT time and then use the toLocaleString method to convert it to the Chatham Time. The Chatham Time has an offset of GMT+12.75.

index.tsx
// Create a Date object with GMT time
const gmtDate = new Date('2022-01-01T00:00:00Z');

// Convert GMT time to Chatham Time
const options = { timeZone: 'Pacific/Chatham' };
const chathamTime = gmtDate.toLocaleString('en-US', options);

console.log(chathamTime); // Output: 1/1/2022, 1:45:00 PM
295 chars
9 lines

In the code above, we created a Date object with GMT time for January 1st, 2022 at midnight. We then used the toLocaleString method to convert the GMT time to the Chatham Time with the timeZone option set to 'Pacific/Chatham'. This option tells JavaScript which time zone to use for the conversion. The output of the console.log statement is the Chatham Time in the format of MM/DD/YYYY, HH:MM:SS AM/PM.

gistlibby LogSnag