javascript日期和时间格式化函数共享

这个功能经常被用来与大家分享。

功能代码:
复制代码代码如下所示:
date.prototype.format =函数(格式){
var { { { {
M +:this.getmonth()+ 1, / /月
D:this.getdate(), / /天
H:this.gethours(), / /小时
M +:this.getminutes(), / /分钟
+:this.getseconds(), / /二
Q+:Math.floor(((this.getmonth()+ 3)/ 3), / /季度
S:this.getmilliseconds() / /毫秒
}
如果( / /(Y +)。试验(格式))格式(1美元= format.replace正则表达式,
(this.getfullyear()+)。Substr(4—9。1美元。长度));
为(O变量K)如果(new RegExp((钾))。试验(格式))
格式(1美元= format.replace正则表达式,
regexp。1美元。长度= = 1 K O { }:
(00+ O { k })。Substr((+ O { k })。长度));
返回格式;
}
举例子:
复制代码代码如下所示:
setInterval(){()函数(
新的日期();
VaR日期=新的日期()。格式(yyyy-mm-dd);
document.getelementbyid().innerHTML =时间;
document.getelementbyid(日期).innerHTML =日期;
},1000);