• 时间日期格式化成需要的形式


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

     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欢迎您来访本博客。此博客是作者在工作中的一个记事本,方便下次遇到同样问题时,以最快的速度解决掉遇到的问题。如果您发现哪里写的不对,欢迎给我留言,让我们一起进步。不胜感激!
  • 相关阅读:
    记一次centos7.2下用crontab执行定时任务的过程(初级)
    海外手机号码正则匹配
    装了wamp之后,80端口被占用解决办法
    newtonsoft动态修改JObject
    .net正则提取手机号码,并替换带有手机号码的a标签
    .vs目录有什么用?
    centos7安装nginx-1.13.6 新手入门,图文解析
    centos7安装kafka_2.11-1.0.0 新手入门
    centos7安装apache 新手入门 图文教程
    面向对象——案例练习(4)判断点是否在圆的内部
  • 原文地址:https://www.cnblogs.com/haley168/p/dateFormat.html
Copyright © 2020-2023  润新知