Last active
December 21, 2024 17:58
-
-
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
This file contains hidden or 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 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')}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx !