现在的位置: 首页 > 综合 > 正文

js时间格式化

2013年10月24日 ⁄ 综合 ⁄ 共 755字 ⁄ 字号 评论关闭

      

       想给这片文章起名为"草船借箭",是因为Java中有强大的时间格式化方法来满足我们各种变态的开发需求,近期写dojo,同样是遇到了这样一个需求,很简单的功能。同样的功能需求,思路很简单,那我们就能把成熟的东西借鉴过来。

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


        format作为Date的属性来使用,只需要把时间对象传参过来即可。这样做也是有弊端的,你想到了吗?顺便提供给大家一个好网站W3school
,
它上面有一个简易的命令行工具

 

抱歉!评论已关闭.