8.7w+
社区成员
/*格式化时间
formatStr:
yyyy:年
MM:月
dd:日
hh:小时
mm:分钟
ss:秒
*/
Date.prototype.toString = function(formatStr)
{
var date = this;
var timeValues = function(){};
timeValues.prototype = {
year:function(){
if(formatStr.indexOf("yyyy")>=0)
{
return date.getYear();
}
else
{
return date.getYear().toString().substr(2);
}
},
elseTime:function(val,formatVal){
return formatVal>=0?(val<10?"0"+val:val):(val);
},
month:function(){
return this.elseTime(date.getMonth ()+1,formatStr.indexOf("MM"));
},
day:function(){
return this.elseTime(date.getDay(),formatStr.indexOf ("dd"));
},
hour:function(){
return this.elseTime(date.getHours(),formatStr.indexOf ("hh"));
},
minute:function(){
return this.elseTime(date.getMinutes (),formatStr.indexOf("mm"));
},
second:function(){
return this.elseTime(date.getSeconds(),formatStr.indexOf ("ss"));
}
}
var tV = new timeValues();
var replaceStr = {
year:["yyyy","yy"],
month:["MM","M"],
day:["dd","d"],
hour:["hh","h"],
minute:["mm","m"],
second:["ss","s"]
};
for(var key in replaceStr)
{
formatStr = formatStr.replace(replaceStr[key][0],eval ("tV."+key+"()"));
formatStr = formatStr.replace(replaceStr[key][1],eval ("tV."+key+"()"));
}
return formatStr;
}
var date = new Date();
alert(date.toString("yyyy-MM-dd hh:mm:ss"));
day:function(){
//date.getDay() 返回date对象中的星期中的天数(0-6) 而不是一个月中的第几天
return this.elseTime(date.getDay(),formatStr.indexOf ("dd"));
}
formatStr = formatStr.replace(replaceStr[key][1],eval ("tV."+key+"()"));