Skip to content

Instantly share code, notes, and snippets.

@simbathesailor
Created January 26, 2020 02:41
Show Gist options
  • Save simbathesailor/5e6f7310449c712262e11a0bcdec9cec to your computer and use it in GitHub Desktop.
Save simbathesailor/5e6f7310449c712262e11a0bcdec9cec to your computer and use it in GitHub Desktop.
getTimezone.js
import { utcToZonedTime, format as formatTimeZone } from 'date-fns-tz';
//'America/Los_Angeles'
// const utcString = '2019-09-25T12:00:00.000Z';
function getTimeZoneText(utcString) {
try {
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (Intl) {
const zoneDateObj = utcToZonedTime(utcString, timeZone);
const timeZoneString = formatTimeZone(zoneDateObj, 'zzzz', { timeZone });
return timeZoneString
.split(' ')
.map(elem => {
return elem.slice(0, 1);
})
.join('');
}
} catch (e) {
console.warn(e);
return '';
}
return '';
}
export default getTimeZoneText;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment