Skip to content

Instantly share code, notes, and snippets.

@gitllermopalafox
Created December 30, 2015 19:57
Show Gist options
  • Select an option

  • Save gitllermopalafox/ae2550ed2786e817d184 to your computer and use it in GitHub Desktop.

Select an option

Save gitllermopalafox/ae2550ed2786e817d184 to your computer and use it in GitHub Desktop.
Function to add 'toYMD()' method to a Date object to get the format 'yyyy-mm-dd' (Mysql date format)
// http://stackoverflow.com/a/2280117/918072
// Function to add 'toYMD()' method to a Date object to get the format 'yyyy-mm-dd'
(function() {
Date.prototype.toYMD = Date_toYMD;
function Date_toYMD() {
var year, month, day;
year = String(this.getFullYear());
month = String(this.getMonth() + 1);
if (month.length == 1) {
month = "0" + month;
}
day = String(this.getDate());
if (day.length == 1) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
})();
// var dt = new Date();
// var str = dt.toYMD();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment