Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Last active September 21, 2021 05:44
Show Gist options
  • Save MatthewDaniels/d54a8c33ef1fa44f8fba5645b341878b to your computer and use it in GitHub Desktop.
Save MatthewDaniels/d54a8c33ef1fa44f8fba5645b341878b to your computer and use it in GitHub Desktop.
Polyifill the JS Date object to have a strict RFC 3339 string method

Date.prototype.toRFC3339String()

Strict RFC3339 does NOT have the milliseconds, but ISO 8601 (Date.prototype.toISOString()) does, so this polyfills it.

if (!Date.prototype.toRFC3339String) {
(function () {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toRFC3339String = function () {
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
'T' + pad(this.getUTCHours()) +
':' + pad(this.getUTCMinutes()) +
':' + pad(this.getUTCSeconds()) +
'Z';
};
}());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment