分类: 系统运维
2009-05-15 16:39:16
var t = new Date(年, 月, 日) 月的取值范围为0-11
当"日"的值大于实际月份天数时自动跳到下一月
如:new Date(2009,3,31) 得2009-5-1
t.toString() //Fri May 1 00:00:00 UTC+0800 2009
t.toLocaleString() //2009年5月1日 0:00:00
还有 t.toLocaleDateString(), t.toLocaleTimeString()
var ms=t.valueOf()=t.getTime() //返回1970-1-1 8:00:00到现在的毫秒数
var t1 = t.setTime(ms+1000) //用毫秒来设置新时间
//格式化 new Date().format("yyyy-MM-dd hh:mm:ss 星期w")
Date.prototype.format = function(style) {
var o = {
"M+" : this.getMonth()+1, //month,从0开始
"d+" : this.getDate(), //day,从1开始
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"w+" : "天一二三四五六".charAt(this.getDay()), //week
"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(style)) {
style = style.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp("("+ k +")").test(style)){
style = style.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
}
}
return style;
};