js Date 时间格式化的扩展

时间:2023-11-13 22:30:02

js Date 时间格式化的扩展:

 Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + , //月
"d+": this.getDate(), //日
"h+": this.getHours() % == ? : this.getHours() % , //时
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + ) / ), //季
"S": this.getMilliseconds() //毫秒
};
var week = {
"": "\u65e5",
"": "\u4e00",
"": "\u4e8c",
"": "\u4e09",
"": "\u56db",
"": "\u4e94",
"": "\u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$, (this.getFullYear() + "").substr( - RegExp.$.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$, ((RegExp.$.length > ) ? (RegExp.$.length > ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}

使用方法如下:

var date = new Date("2016-03-11T16:20:12");
$("#divResult").html(date.format("yyyy-MM-dd EE HH:mm:ss"));