js 格式化时间

时间:2022-03-29 15:25:55
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + ,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + ) / ),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$, (this.getFullYear() + '').substr( - RegExp.$.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$, RegExp.$.length ==
? date[k] : ("" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(new Date().format('yyyy-MM-dd h:m:s'));//2018-03-28 22:03