Skip to content

Instantly share code, notes, and snippets.

@acrogenesis
Last active August 29, 2015 14:16
Show Gist options
  • Save acrogenesis/73e3afc5330aa6789390 to your computer and use it in GitHub Desktop.
Save acrogenesis/73e3afc5330aa6789390 to your computer and use it in GitHub Desktop.
javascript Date toMySqlDate
d = new Date(); // => Thu Feb 26 2015 22:16:14 GMT-0600 (CST)
d.toMySQLDateTime(); // => 2015-02-26 22:16:14
Date.prototype.toMySQLDateTime = function () {
function addZ(n) {
return (n<10? '0' : '') + n;
}
return this.getFullYear() + '-' +
addZ(this.getMonth() + 1) + '-' +
addZ(this.getDate()) + ' ' +
addZ(this.getHours()) + ':' +
addZ(this.getMinutes()) + ':' +
addZ(this.getSeconds());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment