日期格式化函数共享的Javascript实现
复制代码代码如下所示:
日期和时间格式方法原型增加
date.prototype.format =功能(formatstr){
var str = formatstr;
var一周=日,A,二,三,四,五,六};
STR = str.replace( /年| yyyy /,this.getfullyear());
STR = str.replace( / / | YY YY,((this.getyear()% 100)> 9(this.getyear()% 100)。ToString():0+(this.getyear()% 100)));
VaR月= this.getmonth()+ 1;
STR = str.replace( / /毫米,月> 9 month.tostring():0+月);
STR = str.replace( / / g,月);
STR = str.replace( / W | W / g,周{ this.getday())))
STR = str.replace( / / DD | DD,this.getdate()> 9 this.getdate()ToString()):0+ this.getdate()));
STR = str.replace( / D | D / G,this.getdate());
STR = str.replace( / / | HH HH,this.gethours()> 9 this.gethours()ToString()):0+ this.gethours()));
STR = str.replace( / H | H / g,this.gethours());
STR = str.replace( / /毫米,this.getminutes()> 9 this.getminutes()ToString()):0+ this.getminutes()));
STR = str.replace( / / g,this.getminutes());
STR = str.replace( / / | SS SS,this.getseconds()> 9 this.getseconds()ToString()):0+ this.getseconds()));
STR = str.replace( / S | S/g,this.getseconds());
返回字符;
}
调用的时间比较简单,例如:
复制代码代码如下所示:
新日期();
Var STR = d.Format (yyyy-MM-dd hh:mm:ss);
console.log(STR);