javascript 时间格式化
2014-05-16 16:19
190 查看
添加扩展
//时间格式化扩展
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
使用
new Date().format("yyyy-MM-dd hh:mm:ss");
//时间格式化扩展
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
使用
new Date().format("yyyy-MM-dd hh:mm:ss");
相关文章推荐
- javascript格式化时间
- javascript 格式化时间日期函数代码脚本之家修正版
- JavaScript 格式化时间 INT类型 转 字符串时间
- JavaScript图表的时间解析功能(2):日期格式化方法
- JavaScript将时间格式化为yyyy-MM-dd hh:mm:ss
- 用javascript实现input框日期时间格式化输入
- javascript 将时间戳格式化
- javascript时间格式化
- JavaScript时间戳与其格式化
- javascript格式化时间
- 用JavaScript(js)对时间格式化
- javascript格式化时间输出
- javascript时间相加相减格式化
- Javascript 日期时间格式化
- javascript 格式化时间日期函数代码脚本之家修正版
- JavaScript格式化当前时间为yyyy-mm-dd形式
- JavaScript时间戳与其格式化
- 用JavaScript(js)对时间格式化
- JavaScript如何将unix时间戳转化为UTC时间并格式化显示