convert gmt to mountain time in javascript

To convert GMT time to Mountain Time, you can use the Javascript toLocaleString() method with the timeZone option set to America/Denver, which is the time zone for Mountain Time.

Here is an example code block:

index.tsx
// create a new date object from the GMT time
let date = new Date('2022-01-01T12:00:00Z');

// create an options object with timeZone set to America/Denver
let options = { timeZone: 'America/Denver' };

// use the toLocaleString() method with the options object to format the date in Mountain Time
let mountainTime = date.toLocaleString('en-US', options);

console.log(mountainTime); // output: 1/1/2022, 5:00:00 AM
416 chars
11 lines

In this example, we first create a new date object from the GMT time 2022-01-01T12:00:00Z. We then create an options object with the timeZone set to America/Denver. Finally, we use the toLocaleString() method with the options object to format the date in Mountain Time, and log the result to the console. The output should be 1/1/2022, 5:00:00 AM, which is the equivalent time in Mountain Time.

gistlibby LogSnag