Forked from WebReflection/Date.prototype.toDatetimeLocal.js
Created
May 30, 2022 13:16
-
-
Save arubua/8e34adeb46f479172cd4017201c36ac2 to your computer and use it in GitHub Desktop.
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
// https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce | |
Date.prototype.toDatetimeLocal = | |
function toDatetimeLocal() { | |
var | |
date = this, | |
ten = function (i) { | |
return (i < 10 ? '0' : '') + i; | |
}, | |
YYYY = date.getFullYear(), | |
MM = ten(date.getMonth() + 1), | |
DD = ten(date.getDate()), | |
HH = ten(date.getHours()), | |
II = ten(date.getMinutes()), | |
SS = ten(date.getSeconds()) | |
; | |
return YYYY + '-' + MM + '-' + DD + 'T' + | |
HH + ':' + II + ':' + SS; | |
}; | |
Date.prototype.fromDatetimeLocal = (function (BST) { | |
// BST should not be present as UTC time | |
return new Date(BST).toISOString().slice(0, 16) === BST ? | |
// if it is, it needs to be removed | |
function () { | |
return new Date( | |
this.getTime() + | |
(this.getTimezoneOffset() * 60000) | |
).toISOString(); | |
} : | |
// otherwise can just be equivalent of toISOString | |
Date.prototype.toISOString; | |
}('2006-06-06T06:06')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment