JS时间格式化
2016-11-14 14:01
323 查看
/*
* 时间格式化
*/
Date.prototype.format = function (format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (date.hasOwnProperty(k)) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1,
RegExp.$1.length == 1
? date[k]
: ("00" + date[k]).substr(("" + date[k]).length));
}
}
}
return format;
}
* 时间格式化
*/
Date.prototype.format = function (format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (date.hasOwnProperty(k)) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1,
RegExp.$1.length == 1
? date[k]
: ("00" + date[k]).substr(("" + date[k]).length));
}
}
}
return format;
}
相关文章推荐
- js时间格式化
- JS时间格式化
- js扩展Date将时间格式化为"yyyy-MM-dd hh:mm:ss"格式的字符串
- js时间格式化工具类
- js时间格式化
- js 格式化时间日期
- JS 时间格式化
- js时间格式化应用
- 转载:JS时间格式化
- JS格式化时间
- js 格式化时间日期函数小结
- js中格式化时间一位数变成两位数
- JS获得当前时间 并格式化为:yyyy-MM-dd HH:MM:SS
- JS Date格式化日期问题,时间显示区别
- JS 时间格式化
- 在js中对时间类型格式化字符串
- js时间戳、毫秒格式化
- (随笔)js获取当前时间并格格式化当前日期 获取date天后的日期(2014年11月27日 16:31:49)
- js格式化时间
- js时间日期格式化封装函数