Js格式化日期方法

Date.prototype.format = function (pattern) {
var format = pattern || "yyyy-MM-dd HH:mm:ss";
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"H+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"S": this.getMilliseconds(),
"q+": Math.floor((this.getMonth() + 3) / 3) //季度
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};

猜你喜欢

转载自blog.csdn.net/jam_yin/article/details/52396270
今日推荐