Created
April 7, 2022 02:58
-
-
Save nathandines/a89cfa0ebc2a9ff45a2b99c4caf42fe6 to your computer and use it in GitHub Desktop.
Small JS function to output a timezone formatted ISO string
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
function dateToLocalISOString(date) { | |
const tzOffsetMins = date.getTimezoneOffset(); | |
const tzString = (-tzOffsetMins*100/60).toString(); | |
let tzPrefix = ""; | |
if (tzOffsetMins === 0) { | |
return date.toISOString() | |
} else if (tzOffsetMins < 0) { | |
tzPrefix = "+"; | |
} else { | |
tzPrefix = "-"; | |
} | |
return new Date(date.getTime() - (tzOffsetMins * 60 * 1000)) | |
.toISOString() | |
.replace("Z", tzPrefix+tzString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment