数码资讯
日期格式化函数共享的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);
复制代码代码如下所示:
日期和时间格式方法原型增加
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);
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。