Forked from sylvainpolletvillard/valueAsDate.polyfill.js
Created
September 18, 2018 14:33
-
-
Save HenryVonfire/16382e6a1b07d82ed6b1046b5bfd20dc to your computer and use it in GitHub Desktop.
valueAsDate polyfill
This file contains hidden or 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
if(!("valueAsDate" in HTMLInputElement.prototype)){ | |
Object.defineProperty(HTMLInputElement.prototype, "valueAsDate", { | |
get: function(){ | |
var d = this.value.split(/\D/); | |
return new Date(d[0], --d[1], d[2]); | |
}, | |
set: function(d){ | |
var day = ("0" + d.getDate()).slice(-2), | |
month = ("0" + (d.getMonth() + 1)).slice(-2), | |
datestr = d.getFullYear()+"-"+month+"-"+day; | |
this.value = datestr; | |
} | |
}); | |
} | |
// test & demo: http://jsbin.com/wumikawero/1/edit?html,js,output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment