您的位置:首页 > 其它

Date 日期格式化

2016-03-29 11:01 316 查看
<span id="time"></span>
<script>

//名称:日期加法函数
//参数:part(year、month、day、hour、minute、second、millisecond)
//返回:Date对象
Date.prototype.add = function (part, value) {
if (!value || isNaN(value)) value = 0;
switch (part) {
case "year":
this.setFullYear(this.getFullYear() + value);
break;
case "month":
this.setMonth(this.getMonth() + value);
break;
case "day":
this.setDate(this.getDate() + value);
break;
case "hour":
this.setHours(this.getHours() + value);
break;
case "minute":
this.setMinutes(this.getMinutes() + value);
break;
case "second":
this.setSeconds(this.getSeconds() + value);
break;
case "millisecond":
this.setMilliseconds(this.getMilliseconds() + value);
break;
default:
}
return this;
};

Date.prototype.addYears = function (value) {
if (!value || isNaN(value)) value = 0;
this.setFullYear(this.getFullYear() + value);
return this;
};

Date.prototype.addMonths = function (value) {
if (!value || isNaN(value)) value = 0;
this.setMonth(this.getMonth() + value);
return this;
};

Date.prototype.addDays = function (value) {
if (!value || isNaN(value)) value = 0;
this.setDate(this.getDate() + value);
return this;
};

Date.prototype.addHours = function (value) {
if (!value || isNaN(value)) value = 0;
this.setHours(this.getHours() + value);
return this;
};

Date.prototype.addMinutes = function (value) {
if (!value || isNaN(value)) value = 0;
this.setMinutes(this.getMinutes() + value);
return this;
};

Date.prototype.addSeconds = function (value) {
if (!value || isNaN(value)) value = 0;
this.setSeconds(this.getSeconds() + value);
return this;
};

Date.prototype.addMilliseconds = function (value) {
if (!value || isNaN(value)) value = 0;
this.setMilliseconds(this.getMilliseconds() + value);
return this;
};

//名称:日期加法函数
//参数:time(日期字符串,示例:12:00:00)
//返回:Date对象
Date.prototype.addTime = function (time) {
var timeRegex = /^([0-1]?\d|2[0-3])(:[0-5]?\d){1,2}$/g;
if (timeRegex.test(time)) {
var value = Date.parse("1970/1/1 " + time) - Date.parse("1970/1/1");
this.setMilliseconds(this.getMilliseconds() + value);
}
return this;
};

//名称:日期格式化函数
//参数:format(示例:yyyy-MM-dd hh:mm:ss)、zeroize(是否补零)
//返回:日期字符串
Date.prototype.toCustomString = function (format, zeroize) {
if (!zeroize) zeroize = false;
var dy = this.getFullYear();
var dM = this.getMonth() + 1;
var dd = this.getDate();
var dh = this.getHours();
var dm = this.getMinutes();
var ds = this.getSeconds();
var dS = this.getMilliseconds();
var orm = {
"y+": dy.toString(),
"M+": !zeroize ? dM.toString() : dM < 10 ? '0' + dM : dM.toString(),
"d+": !zeroize ? dd.toString() : dd < 10 ? '0' + dd : dd.toString(),
"h+": !zeroize ? dh.toString() : dh < 10 ? '0' + dh : dh.toString(),
"m+": !zeroize ? dm.toString() : dm < 10 ? '0' + dm : dm.toString(),
"s+": !zeroize ? ds.toString() : ds < 10 ? '0' + ds : ds.toString(),
"S": dS.toString()
};
for (var i in orm) {
var patt = new RegExp(i);
if (patt.test(format)) {
var item = orm[i];
var ms = format.match(patt);
var result = ms[0];
if (i === "S") {
format = format.replace(result, item);
} else {
format = format.replace(result, item.substr(item.length - result.length));
}
}
}
return format;
};
window.onload = function(){
var time = document.getElementById("time");

setInterval('time.innerText = new Date().toCustomString("yyyy-MM-dd hh:mm:ss")',1000);

}

</script>


没有格式啥的要求的话,就用Date下的toLocaleString()显示年月日时间或者toLocaleDateString()显示年月日

<span id="time"></span>
<script>
var time = document.getElementById("time");
setInterval('time.innerHTML = new Date().toLocaleString()', 1000);
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: