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轉換時間戳。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM