convert mountain time to gmt in javascript

Here's a function that can convert a date object in Mountain Time to GMT:

index.tsx
function convertMountainToGMT(date) {
  // Get the timezone offset for Mountain Time in minutes
  const mtOffset = -420;
  
  // Convert the date to UTC time by adding the timezone offset
  const utcTime = date.getTime() + (date.getTimezoneOffset() * 60000);
  
  // Convert the UTC time to GMT by subtracting the timezone offset for Mountain Time
  const gmtTime = new Date(utcTime - (mtOffset * 60000));
  
  return gmtTime;
}
429 chars
13 lines

To use this function, simply pass in a date object in Mountain Time:

index.tsx
const mtDate = new Date('2021-07-15T12:00:00-06:00');
const gmtDate = convertMountainToGMT(mtDate);
console.log(gmtDate.toUTCString()); // Outputs: 'Thu, 15 Jul 2021 18:00:00 GMT'
180 chars
4 lines

Note that Mountain Time is represented by a negative timezone offset (currently -6 hours), so we add this offset in minutes to the date's UTC time to get the local time in Mountain Time. We then subtract this offset from the UTC time to get the equivalent time in GMT.

gistlibby LogSnag