js 日期格式化 函数

function formatDate(date,format){
         var paddNum = function(num){
             num += "";
             return num.replace(/^(\d)$/,"0$1");
         }
         //指定格式字符
         var cfg = {
             yyyy : date.getFullYear() //年 : 4位
             ,yy : date.getFullYear().toString().substring(2)//年 : 2位
             ,M  : date.getMonth() + 1  //月 : 如果1位的时候不补0
             ,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
             ,d  : date.getDate()   //日 : 如果1位的时候不补0
             ,dd : paddNum(date.getDate())//日 : 如果1位的时候补0
             ,hh : paddNum(date.getHours()+1)  //
             ,mm : paddNum(date.getMinutes()+1) //
             ,ss : paddNum(date.getSeconds()+1) //
         }
         format || (format = "yyyy-MM-dd hh:mm:ss");
         return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});
     }

 

posted @ 2016-09-07 11:30  Ali.Blacken  阅读(355)  评论(0编辑  收藏  举报