Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HenryVonfire/16382e6a1b07d82ed6b1046b5bfd20dc to your computer and use it in GitHub Desktop.
Save HenryVonfire/16382e6a1b07d82ed6b1046b5bfd20dc to your computer and use it in GitHub Desktop.
valueAsDate polyfill
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