Skip to content

Instantly share code, notes, and snippets.

@MegaMinxCoding
Last active January 15, 2023 13:11
Show Gist options
  • Select an option

  • Save MegaMinxCoding/3b6bdd742a43204814c16a3a38f3f604 to your computer and use it in GitHub Desktop.

Select an option

Save MegaMinxCoding/3b6bdd742a43204814c16a3a38f3f604 to your computer and use it in GitHub Desktop.
Create an actual ISO string referring to the local TZ
function toIsoString(date_param) {
if (date_param === undefined || date_param === null)return ''
if(!((date_param instanceof Date) || (typeof date_param ==="string"))) throw new Error(`Illegal Argument of type ${typeof date_param} on method toIsoString! Expected date or string.`)
let date = new Date(date_param)
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function (num) {
return (num < 10 ? '0' : '') + num
}
return (
date.getFullYear() +
'-' +
pad(date.getMonth() + 1) +
'-' +
pad(date.getDate()) +
'T' +
pad(date.getHours()) +
':' +
pad(date.getMinutes()) +
':' +
pad(date.getSeconds()) +
dif +
pad(Math.floor(Math.abs(tzo) / 60)) +
':' +
pad(Math.abs(tzo) % 60)
)
}
console.log(new Date().toISOString()) // 2023-01-11T21:32:01.130Z
console.log(toIsoString(new Date())) // 2023-01-11T22:32:01+01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment