Chinaunix首页 | 论坛 | 博客
  • 博客访问: 568276
  • 博文数量: 190
  • 博客积分: 10937
  • 博客等级: 上将
  • 技术积分: 2205
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-07 11:28
文章分类

全部博文(190)

文章存档

2012年(1)

2011年(27)

2010年(20)

2009年(142)

我的朋友

分类: 系统运维

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;
};

// "2008-1-1".toDate()
String.prototype.toDate = function(x, p) {
 if (this.length==0) return null;
 if(x == null) x = "-";  // 分隔符
 if(p == null) p = "ymd"; //年月日顺序
 var a = this.split(x);
 if (a.length<3) return null;
 var y = parseInt(a[p.indexOf("y")],10);
 var m = parseInt(a[p.indexOf("m")],10);
 var d = parseInt(a[p.indexOf("d")],10);
 if(isNaN(y) || isNaN(m) || isNaN(d)) return null;
 if(y.toString().length <= 2) y += 2000;
 return new Date(y, m-1, d);
}
阅读(548) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~