For example, if the output of the api is "13:29" and mountain daylight time is -6 UTC, how would I go about subtracting 6 from the 13 hour mark? I already have the local UTC time difference worked out below.
const date = new Date();
let timeDifference = date.getTimezoneOffset() / 60;
You can get the local time by setting the UTC hours/minutes of a new date.
const utcTimeToLocalTime = (utcTime, hour12 = false) => {
const [hour, minute] = utcTime.split(':').map(v => parseInt(v, 10));
const now = new Date();
now.setUTCHours(hour);
now.setUTCMinutes(minute);
return now.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12
});
};
console.log(utcTimeToLocalTime('13:29')); // 09:29 (EDT) or 06:29 (PDT)
console.log(utcTimeToLocalTime('13:29', true)); // 09:29 AM (EDT) or 06:29 AM (PDT)