Last active
May 27, 2024 12:25
-
-
Save pjdietz/e0545332e2fc67a9a460 to your computer and use it in GitHub Desktop.
Format a local date as an RFC 3339 date with timezone
This file contains 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 rfc3339(d) { | |
function pad(n) { | |
return n < 10 ? "0" + n : n; | |
} | |
function timezoneOffset(offset) { | |
var sign; | |
if (offset === 0) { | |
return "Z"; | |
} | |
sign = (offset > 0) ? "-" : "+"; | |
offset = Math.abs(offset); | |
return sign + pad(Math.floor(offset / 60)) + ":" + pad(offset % 60); | |
} | |
return d.getFullYear() + "-" + | |
pad(d.getMonth() + 1) + "-" + | |
pad(d.getDate()) + "T" + | |
pad(d.getHours()) + ":" + | |
pad(d.getMinutes()) + ":" + | |
pad(d.getSeconds()) + | |
timezoneOffset(d.getTimezoneOffset()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!