Skip to content

Instantly share code, notes, and snippets.

@loilo
Last active September 12, 2023 07:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loilo/736d5beaef4a96d652f585b1b678a12c to your computer and use it in GitHub Desktop.
Save loilo/736d5beaef4a96d652f585b1b678a12c to your computer and use it in GitHub Desktop.
ISO 8601 date string – like Date.prototype.toISOString(), but with local timezone offset
function getLocalISOString(date) {
const offset = date.getTimezoneOffset()
const offsetAbs = Math.abs(offset)
const isoString = new Date(date.getTime() - offset * 60 * 1000).toISOString()
return `${isoString.slice(0, -1)}${offset > 0 ? '-' : '+'}${String(Math.floor(offsetAbs / 60)).padStart(2, '0')}:${String(offsetAbs % 60).padStart(2, '0')}`
}
@jniac
Copy link

jniac commented Jan 22, 2022

thx !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment