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欢迎您来访本博客。此博客是作者在工作中的一个记事本,方便下次遇到同样问题时,以最快的速度解决掉遇到的问题。如果您发现哪里写的不对,欢迎给我留言,让我们一起进步。不胜感激!
  • 相关阅读:
    判断是否在可视区域
    格式化数字10000,为10,000
    把数组(含有30项分成5组)
    排序
    enzyme 学习一
    golang使用sftp连接服务器远程上传、下载文件
    golang使用ssh远程连接服务器并执行命令
    一文弄懂vlan、三层交换机、网关、DNS、子网掩码、MAC地址的含义
    golang的序列化与反序列化的几种方式
    golang命令行参数解析
  • 原文地址:https://www.cnblogs.com/haley168/p/dateFormat.html
Copyright © 2011-2022 走看看