2008-04-16

js中好用時間格式化函數

关键字: javascript
Date.prototype.format = function(format)
{
   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;
}


可以這樣調用:
引用
showtime = new Date().format("yyyy-MM-dd hh:mm:ss");


和java中格式化時間一樣 超好用!
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

morris
搜索本博客
存档
最新评论