Skip to content

Instantly share code, notes, and snippets.

@ARHEIO
Created October 2, 2019 02:20
Show Gist options
  • Save ARHEIO/1f0699daf9e84ee960e48755b75bde15 to your computer and use it in GitHub Desktop.
Save ARHEIO/1f0699daf9e84ee960e48755b75bde15 to your computer and use it in GitHub Desktop.
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