zl程序教程

您现在的位置是:首页 >  后端

当前栏目

扩展JSDate对象时间格式化功能的小例子

扩展对象 功能 时间 例子 格式化 JSdate
2023-06-13 09:15:13 时间

在自己JS代码中引入一下代码:

复制代码代码如下:

Date.prototype.format=function(format)
{
   varo={
           "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(varkino)
   {
       if(newRegExp("("+k+")").test(format))
       {
           format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length));
       }
   }
   returnformat;
};

调用方法如下:

vardt=newDate();
varnowDate=dt.format("yyyy-MM-ddhh:mm:ss");

就能得到当前的时间:2013-12-0214:02:11