Skip to content

Instantly share code, notes, and snippets.

@jackey
Created May 7, 2013 06:21
Show Gist options
  • Save jackey/5530591 to your computer and use it in GitHub Desktop.
Save jackey/5530591 to your computer and use it in GitHub Desktop.
nodejs date to mysql datetime format
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();
}
 
Date.prototype.toMysqlFormat = function() {
    return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
 
var MyDate = new Date();
MyDate.toMysqlFormat(); //return MySQL Datetime format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment