Created
January 26, 2020 02:41
-
-
Save simbathesailor/5e6f7310449c712262e11a0bcdec9cec to your computer and use it in GitHub Desktop.
getTimezone.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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