/**
* 扩展格式化时间函数
* @param fmt -- 只接受字符串{yyyy-MM-dd | HH:mm:ss}
* @returns {*}
* @constructor
*/
Date.prototype.format = function (fmt) {
if (typeof fmt != 'string') {
throw new Error('参数格式不正确');
}
this.o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds() //秒
};
this.k = this.k || (this.k = 0);
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (this.k in this.o)
if (new RegExp("(" + this.k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (this.o[this.k]) : (("00" + this.o[this.k]).substr(("" + this.o[this.k]).length)));
}
this.k = null;
this.o = null;
return fmt;
};
js模拟格式化时间
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- (一) Date对象 概述 Date对象是JavaScript提供的日期和时间的操作接口。它可以表示的时间范围是,...