js格式化日期格式

使用上述的方法即可格式化日期

<html>
  <head>
    <script language="javascript">
      function Window_Load(){
        var str = "Tue Jul 16 01:07:00 CST 2013";
        alert(formatCSTDate(str,"yyyy-M-d")); //2013-7-16 16:24:58
         
       alert(formatDate((new Date()),"yyyy-MM-dd")); //2013-07-15 
        alert(formatDate((new Date()),"yyyy/M/d")); //2013/7/15 
      } 
       
      //格式化CST日期的字串
      function formatCSTDate(strDate,format){
        return formatDate(new Date(strDate),format);
      }
       
      //格式化日期,
      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 : date.getHours()  //时
          ,mm : date.getMinutes() //分
          ,ss : date.getSeconds() //秒
        }
        format || (format = "yyyy-MM-dd hh:mm:ss");
        return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});
      } 
    </script>
  </head>
  <body οnlοad="Window_Load();">
   
  </body>
</html>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值