有人知道如何将JS dateTime转换为MySQL dateTime吗?还有没有办法向JS datetime添加特定的分钟数,然后将其传递给MySQL datetime?
有人知道如何将JS dateTime转换为MySQL dateTime吗?还有没有办法向JS datetime添加特定的分钟数,然后将其传递给MySQL datetime?
虽然JS拥有足够的基本工具来实现这一点,但它相当笨重.
/**
* You first need to create a formatting function to pad numbers to two digits…
**/
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();
}
/**
* …and then create the method to output the date string as desired.
* Some people hate using prototypes this way, but if you are going
* to apply this to more than one Date object, having it as a prototype
* makes sense.
**/
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};