zoukankan      html  css  js  c++  java
  • 时间日期格式化成需要的形式

    --------------------------------------------------------------------------------------------------------------------------------

     1 <!doctype html>
     2 <html lang="en">
     3  <head>
     4   <meta charset="UTF-8">
     5   <meta name="Generator" content="EditPlus®">
     6   <meta name="Author" content="">
     7   <meta name="Keywords" content="">
     8   <meta name="Description" content="">
     9   <script src="http://192.168.1.202/1_exer/0_resources/jq/jquery-1.11.3.js"></script>
    10   <title>时间日期相关</title>
    11  </head>
    12  <body>
    13   <script>
    14     /*  时间对象的格式化 */
    15     Date.prototype.format = function(format){
    16         var o = {
    17             "M+" : this.getMonth() + 1,
    18             "d+" : this.getDate(),
    19             "h+" : this.getHours(),
    20             "m+" : this.getMinutes(),
    21             "s+" : this.getSeconds(),
    22             "q+" : Math.floor((this.getMonth() + 3) / 3),
    23             "S" : this.getMilliseconds()
    24         }
    25         if (/(y+)/.test(format)){
    26             format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
    27                     - RegExp.$1.length)); }
    28         for (var k in o){
    29             if (new RegExp("(" + k + ")").test(format)){
    30                 format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]: ("00" + o[k]).substr(("" + o[k]).length));}  }
    31         return format;
    32     }
    33     //以下为调用
    34     $(function(){
    35         var date=new Date();
    36         var dateFormat=date.format('yyyy-MM-dd hh:mm:ss');
    37         var dateFormat3=date.format('yyyy/MM/dd hh:mm:ss');
    38         $('.dateTime1').html(date);
    39         $('.dateTime2').html(dateFormat);
    40         $('.dateTime3').html(dateFormat3);
    41         console.log(234);
    42     });
    43   </script>
    44   <p>格式化前:<span class="dateTime1"></span></p>
    45   <p>格式化yyyy-MM-dd hh:mm:ss后:<span class="dateTime2"></span></p>
    46   <p>格式化yyyy/MM/dd hh:mm:ss后:<span class="dateTime3"></span></p>
    47  </body>
    48 </html>
    haley欢迎您来访本博客。此博客是作者在工作中的一个记事本,方便下次遇到同样问题时,以最快的速度解决掉遇到的问题。如果您发现哪里写的不对,欢迎给我留言,让我们一起进步。不胜感激!
  • 相关阅读:
    十、CI框架之通过参数的办法输出URI路径
    九、CI框架之将URI转为数组原型
    八、CI框架之输出URI路径,相当于GET
    七、CI框架之分配变量数组,循环输出
    六、CI框架之分配变量
    五、CI框架之通过带路径的view视图路径访问
    四、CI框架之通过URL路径访问C中的函数
    三、CI框架之一个最简单的页面
    二、CI框架之MCV模型
    一、CI框架(CodeIgniter)简介
  • 原文地址:https://www.cnblogs.com/haley168/p/dateFormat.html
Copyright © 2011-2022 走看看