Created
October 2, 2019 02:20
-
-
Save ARHEIO/1f0699daf9e84ee960e48755b75bde15 to your computer and use it in GitHub Desktop.
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
const date = new Date(); | |
function createProperISOString(date) { | |
const dateTime = date.toISOString().slice(0, 19); | |
const timezoneOffset = -date.getTimezoneOffset() / 60; | |
let timezoneOffsetAsHours | |
if (Number.isInteger(timezoneOffset)) { | |
timezoneOffsetAsHours = `${('0' + Math.abs(timezoneOffset)).slice(-2)}:00`; | |
} else { | |
timezoneOffsetAsHours = `${('0' + Math.abs(Math.floor(timezoneOffset))).slice(-2)}:30`; | |
} | |
timezoneOffsetAsHours = timezoneOffset < 0 ? `-${timezoneOffsetAsHours}` : `+${timezoneOffsetAsHours}`; | |
return dateTime + timezoneOffsetAsHours; | |
} | |
const dateInISOWithTimezone = createProperISOString(date); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment