1. 程式人生 > >格式化時間,new Date()兼容IE

格式化時間,new Date()兼容IE

gets prot function new lac etsec ise hour bre

----------------------------------------------------------new Date()兼容IE
var date = new Date(strDate.replace(/-/g, "/"));


---------------------------------------------------------格式化時間
Date.prototype.format = function(format)
{
/*
* format="yyyy-MM-dd hh:mm:ss";
*/
var o = {
"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+)/.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;
}

//去掉空格
function trim(str){
str = str.replace(/^(\s|\u00A0)+/,‘‘);
for(var i=str.length-1; i>=0; i--){
if(/\S/.test(str.charAt(i))){
str = str.substring(0, i+1);
break;
}
}
return str;
}

格式化時間,new Date()兼容IE