meizz的专栏

梅花雪·疏影横斜

原创 时间对象的格式化

新一篇: javascript里的条件判断

<script language="JavaScript">
Date.prototype.format = function(format) //author: meizz
{
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
  }
  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;
}
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
</script>

发表于 @ 2005年06月28日 15:01:00|评论(loading...)|编辑

旧一篇: meizz(梅花雨) 网页日历 3.0

评论

#lee 发表于2005-12-11 20:36:00  IP: 211.138.29.*
谢谢楼主。。。偶又学了一招
#chnechen 发表于2006-01-10 10:51:00  IP: 218.63.185.*
很实用的代码,朋友如不介意,个人已把此代码转帖于我的blog..谢谢!
#meizz 发表于2008-03-11 07:15:44  IP: 58.31.24.*
http://deals.qunar.com/list.jcp?noCategory=1&_from=%E5%8C%97%E4%BA%AC&_to=%E6%9D%AD%E5%B7%9E
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © meizz