Skip to content

Instantly share code, notes, and snippets.

@jonathanlurie
Created June 25, 2020 12:49
Show Gist options
  • Save jonathanlurie/9cde03aee415f3b5ecdd88cb85a86552 to your computer and use it in GitHub Desktop.
Save jonathanlurie/9cde03aee415f3b5ecdd88cb85a86552 to your computer and use it in GitHub Desktop.
// This comes from https://usefulangle.com/post/30/javascript-get-date-time-with-offset-hours-minutes
function getIso8601z() {
// timezone part
const timezoneOffsetMin = new Date().getTimezoneOffset()
const offsetHours = parseInt(Math.abs(timezoneOffsetMin / 60))
const offsetMinutes = Math.abs(timezoneOffsetMin % 60)
let timezoneStandard = null
if(offsetHours < 10) {
offsetHours = `0${offsetHours}`
}
if(offsetMinutes < 10) {
offsetMinutes = `0${offsetMinutes}`
}
// Add an opposite sign to the offset
// If offset is 0, it means timezone is UTC
if(timezoneOffsetMin < 0) {
timezoneStandard = `+${offsetHours}:${offsetMinutes}`
} else if(timezoneOffsetMin > 0) {
timezoneStandard = `-${offsetHours}:${offsetMinutes}`
} else if(timezoneOffsetMin == 0) {
timezoneStandard = 'Z'
}
// date part
const dt = new Date()
let currentDate = dt.getDate()
let currentMonth = dt.getMonth() + 1
let currentYear = dt.getFullYear()
let currentHours = dt.getHours()
let currentMinutes = dt.getMinutes()
let currentSeconds = dt.getSeconds()
let currentDatetime = null
// Add 0 before date, month, hrs, mins or secs if they are less than 0
currentDate = currentDate < 10 ? `0${currentDate}` : currentDate;
currentMonth = currentMonth < 10 ? `0${currentMonth}` : currentMonth;
currentHours = currentHours < 10 ? `0${currentHours}` : currentHours;
currentMinutes = currentMinutes < 10 ? `0${currentMinutes}` : currentMinutes;
currentSeconds = currentSeconds < 10 ? `0${currentSeconds}` : currentSeconds;
// Current datetime
// String such as 2016-07-16T19:20:30
currentDatetime = `${currentYear}-${currentMonth}-${currentDate}T${currentHours}:${currentMinutes}:${currentSeconds}`
return `${current_datetime}${timezone_standard}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment