Skip to content

Instantly share code, notes, and snippets.

@ophentis
Created October 24, 2012 03:12
Show Gist options
  • Save ophentis/3943464 to your computer and use it in GitHub Desktop.
Save ophentis/3943464 to your computer and use it in GitHub Desktop.
javascript Date object produce mysql format output
Date.prototype.toMysqlFormat = function() {
function twoDigits(d) {
if(0 <= d && d < 10) return '0' + d.toString();
if(-10 < d && d < 0) return '-0' + (-1*d).toString();
return d.toString();
}
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment