天天看點

mysql日期轉換為時間_将JS日期時間轉換為MySQL日期時間

mysql日期轉換為時間_将JS日期時間轉換為MySQL日期時間

Does anyone know how to convert JS dateTime to MySQL datetime? Also is there a way to add a specific number of minutes to JS datetime and then pass it to MySQL datetime?

解決方案

While JS does possess enough basic tools to do this, it's pretty clunky.

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.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());

};