JS 格式化日期

| |
[不指定 2016/07/15 21:03 | by 刘新修 ]
JavaScript代码
  1. /***** 格式化日期 *****/  
  2. function formatDate(_strTime,_format){  
  3.     var date=new Date(_strTime);  
  4.     var paddNum=function(num){  
  5.         num+="";  
  6.         return num.replace(/^(\d)$/,"0$1");  
  7.     }  
  8.     //指定格式字符  
  9.     var cfg={  
  10.         yyyy:date.getFullYear()                         //年:4位  
  11.         ,yy :date.getFullYear().toString().substring(2) //年:2位  
  12.         ,M  :date.getMonth() + 1                        //月:如果1位的时候不补0  
  13.         ,MM :paddNum(date.getMonth()+1)                 //月:如果1位的时候补0  
  14.         ,d  :date.getDate()                             //日:如果1位的时候不补0  
  15.         ,dd :paddNum(date.getDate())                    //日:如果1位的时候补0  
  16.         ,hh :date.getHours()                            //时  
  17.         ,mm :date.getMinutes()                          //分  
  18.         ,ss :date.getSeconds()                          //秒  
  19.     }  
  20.     _format||(_format="yyyy-MM-dd hh:mm:ss");  
  21.     return _format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});  
  22. }  
  23. formatDate("Tue Jul 16 01:07:00 CST 2013","yyyy-MM-dd ");  
H5/JS/CSS | 评论(0) | 引用(0) | 阅读(2890)