Skip to content

Instantly share code, notes, and snippets.

@nathandines
Created April 7, 2022 02:58
Show Gist options
  • Save nathandines/a89cfa0ebc2a9ff45a2b99c4caf42fe6 to your computer and use it in GitHub Desktop.
Save nathandines/a89cfa0ebc2a9ff45a2b99c4caf42fe6 to your computer and use it in GitHub Desktop.
Small JS function to output a timezone formatted ISO string
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