Created
July 14, 2021 08:35
-
-
Save chzager/4da2f9dd88ba4e5e9a1a5fcc4d637c44 to your computer and use it in GitHub Desktop.
javascript date toISOString with timezone
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 localeIsoDateString(date = new Date()) | |
{ | |
let timezoneOffsetHours = (new Date()).getTimezoneOffset() * 60 * 1000; | |
let timezoneAsString = ["+", "+", "-"][Math.sign(timezoneOffsetHours) + 1] + (new Date(Math.abs(timezoneOffsetHours)).toISOString().substr(11, 5)); | |
return new Date(date.getTime() - timezoneOffsetHours).toISOString().substr(0, 23) + timezoneAsString; | |
}; | |
// Expected output (for Central European Summer Time): 2021-07-14T10:34:01.830+02:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment