• js如何将时间戳转换为标准时间


     1 function formatDate(date,fmt){
     2     let o = {
     3         'M+' : date.getMonth() +1,                    //月份
     4       'd+' : date.getDate(),                        //日
     5       'h+' : date.getHours(),                       //小时
     6       'm+' : date.getMinutes(),                     //分  
     7       's+' : date.getSeconds(),                     //秒
     8       "q+":  Math.floor((date.getMonth() + 3) / 3), //季度   
     9         "S":   date.getMilliseconds()                 //毫秒   
    10     };
    11     if(/(y+)/.test(fmt)){   //年份
    12       fmt = fmt.replace(RegExp.$1,(date.getFullYear()+'').substr(4-RegExp.$1.length));
    13     }
    14     for(let k in o){
    15      if (new RegExp("(" + k + ")").test(fmt)){
    16         let str = o[k] + '';
    17         fmt = fmt.replace(RegExp.$1,(RegExp.$1.length === 1) ? str:padLeftZero(str));
    18       }
    19     }
    20     return fmt;
    21 };
    22 
    23 function padLeftZero(str){
    24     return ('00'+str).substr(str.length);
    25 }
    26 
    27     let date= new Date(1469281964000); //此为时间戳
    28     console.log(formatDate(date, 'yyyy-MM-dd hh:mm'));

      一般情况下,后台保存在数据库的时间格式为时间戳,在通过接口传递的时候后台会转换时间戳为时间格式,那么如果没有转换的话,前端也可以通过js转换时间戳。

  • 相关阅读:
    day4
    day3
    day2
    day1
    spring-boot-note
    spring-boot-cli
    jquery ajax rest invoke
    spring-boot
    docker mysql
    jpa OneToMany
  • 原文地址:https://www.cnblogs.com/xueweijie/p/7190633.html
Copyright © 2020-2023  润新知