/**
* js时间对象的格式化;
* eg:format="yyyy-MM-dd hh:mm:ss";
*/
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
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;
}
/**
*js中更改日期
* y年, m月, d日, h小时, n分钟,s秒
*/
Date.prototype.add = function (part, value) {
value *= 1;
if (isNaN(value)) {
value = 0;
}
switch (part) {
case "y":
this.setFullYear(this.getFullYear() + value);
break;
case "m":
this.setMonth(this.getMonth() + value);
break;
case "d":
this.setDate(this.getDate() + value);
break;
case "h":
this.setHours(this.getHours() + value);
break;
case "n":
this.setMinutes(this.getMinutes() + value);
break;
case "s":
this.setSeconds(this.getSeconds() + value);
break;
default:
}
}
js实现类似C# 时间格式化、时间运算
最新推荐文章于 2024-05-11 11:39:06 发布