js时间类Date扩展
2017-01-04 10:30
162 查看
//格式化
Date.prototype.format=function(fmt) {
if(!fmt){
fmt = "yyyy-MM-dd HH:mm:ss";
}
var o = {
"M+" : this.getMonth()+1, //Month
"d+" : this.getDate(), //Day
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //12 hour
"H+" : this.getHours(), //24 hour
"m+" : this.getMinutes(), //Minute
"s+" : this.getSeconds(), //Second
"q+" : Math.floor((this.getMonth()+3)/3), //Quarter
"S" : this.getMilliseconds(), //Millisecond
't+' : this.getHours() < 12 ? 'am' : 'pm',
'T+' : this.getHours() < 12 ? 'AM' : 'PM'
};
var week = {
"0" : "Sunday",
"1" : "Monday",
"2" : "Tuesday",
"3" : "Wednesday",
"4" : "Thursday",
"5" : "Friday",
"6" : "Saturday"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, week[this.getDay()+""]);
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
Date.prototype.format=function(fmt) {
if(!fmt){
fmt = "yyyy-MM-dd HH:mm:ss";
}
var o = {
"M+" : this.getMonth()+1, //Month
"d+" : this.getDate(), //Day
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //12 hour
"H+" : this.getHours(), //24 hour
"m+" : this.getMinutes(), //Minute
"s+" : this.getSeconds(), //Second
"q+" : Math.floor((this.getMonth()+3)/3), //Quarter
"S" : this.getMilliseconds(), //Millisecond
't+' : this.getHours() < 12 ? 'am' : 'pm',
'T+' : this.getHours() < 12 ? 'AM' : 'PM'
};
var week = {
"0" : "Sunday",
"1" : "Monday",
"2" : "Tuesday",
"3" : "Wednesday",
"4" : "Thursday",
"5" : "Friday",
"6" : "Saturday"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, week[this.getDay()+""]);
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
相关文章推荐
- JS 时间格式化 对Date的扩展,将 Date 转化为指定格式的String
- 扩展JS Date对象时间格式化功能的小例子
- js date时间格式化扩展
- js日期时间格式化函数 支持季度 24/12小时制 周 毫秒(Date扩展)
- 扩展JS Date对象时间格式化功能
- js扩展Date将时间格式化为"yyyy-MM-dd hh:mm:ss"格式的字符串
- js Date 时间格式化的扩展
- 扩展JS Date对象时间格式化功能的小例子
- js 扩展jquery date 转换时间为UTC时间
- js Date对象扩展
- JS时间对象Date以及时间戳
- js时间操作扩展方法
- js 时间转成时间戳对比;My97DatePicker日历控件时间格式;Date.parse Firefox火狐浏览器返回Nan的解决办法
- js中获取时间new Date()详细介绍
- js中获取时间new Date()详细介绍
- js用Date对象处理时间
- js用Date对象处理时间实现思路及代码
- 经常要用到JS查询时间,附上js中new Date()详细介绍。
- C#DateTime时间与JS Date时间的相互转化(C#后台)
- JS将字符串类型的时间格式转换为date格式