zoukankan      html  css  js  c++  java
  • js 格式化时间

     1 /**     
     2  *转换long值为日期字符串     
     3  * @param l long值     
     4  * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss     
     5  * @return 符合要求的日期字符串     
     6  */      
     7 invalid Date 是 new Date(l)中的l是字符串发生的异常
     8  function getFormatDateByLong(l) {
     9      if(l==''){
    10          return "";
    11      }else{
    12         l=parseInt(l);
    13      return getFormatDate(new Date(l), "yyyy-MM-dd hh:mm:ss");
    14      }
    15  }    
    16  /**     
    17  *转换日期对象为日期字符串     
    18  * @param l long值     
    19  * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss     
    20  * @return 符合要求的日期字符串     
    21  */      
    22  function getFormatDate(date, pattern) {    
    23      if (date == undefined) {    
    24          date = new Date();    
    25      }    
    26      if (pattern == undefined) {    
    27          pattern = "yyyy-MM-dd hh:mm:ss";    
    28      }    
    29      return date.format(pattern);    
    30  }  
    31 //格式化json时间类型
    32 function toDate(objDate) {
    33     var date = new Date();
    34     date.setTime(objDate.time);
    35     date.setHours(objDate.hours);
    36     date.setMinutes(objDate.minutes);
    37     date.setSeconds(objDate.seconds);
    38     return date.format("yyyy-MM-dd HH:mm:ss");
    39 }
    40 Date.prototype.format = function(format) {
    41     /*
    42      * format="yyyy-MM-dd hh:mm:ss";
    43      */
    44     var o = {
    45         "M+" : this.getMonth() + 1,
    46         "d+" : this.getDate(),
    47         "h+" : this.getHours(),
    48         "m+" : this.getMinutes(),
    49         "s+" : this.getSeconds(),
    50         "q+" : Math.floor((this.getMonth() + 3) / 3),
    51         "S" : this.getMilliseconds()
    52     };
    53     if (/(y+)/.test(format)) {
    54             format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4- RegExp.$1.length));
    55         }
    56     for (var k in o) {
    57         if (new RegExp("(" + k + ")").test(format)){
    58             format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]:("00" + o[k]).substr(("" + o[k]).length));
    59         }
    60     }
    61     return format;
    62     
    63 };
  • 相关阅读:
    vscode常用插件列表
    使用docker构建supervisor全步骤
    docker删除虚悬镜像(临时镜像文件)
    消息队列的对比
    ECharts使用:this.dom.getContext is not a function
    curl命令行请求
    工作工具清单
    《SQL优化入门》讲座总结
    初始化git库并配置自动部署
    php代码进行跨域请求处理
  • 原文地址:https://www.cnblogs.com/-lpf/p/4923232.html
Copyright © 2011-2022 走看看