js 格式化時間戳


js格式化時間戳,根據傳入時間格式返回相應格式的時間

 1 function (date = 0, fmt = 'yyyy-MM-dd hh:mm:ss') {
 2     
 3     date = new Date(+date)
 4     if (/(y+)/.test(fmt)) {
 5       fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
 6     }
 7     let o = {
 8       'M+': date.getMonth() + 1,
 9       'd+': date.getDate(),
10       'h+': date.getHours(),
11       'm+': date.getMinutes(),
12       's+': date.getSeconds()
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 : ('00' + str).substr(str.length));
18       }
19     }
20     return fmt;
21   }

以上


免責聲明!

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



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