vue項目里的日期格式化


在項目中,我們經常需要把后台傳回的日期進行格式化,可以在common里定義一個公共的js

export function formatDate (date, fmt) {
 2   if (/(y+)/.test(fmt)) {
 3     fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
 4   }
 5   let o = {
 6     'M+': date.getMonth() + 1,
 7     'd+': date.getDate(),
 8     'h+': date.getHours(),
 9     'm+': date.getMinutes(),
10     's+': date.getSeconds()
11   }
12   for (let k in o) {
13     if (new RegExp(`(${k})`).test(fmt)) {
14       let str = o[k] + ''
15       fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
16     }
17   }
18   return fmt
19 }
20 
21 function padLeftZero (str) {
22   return ('00' + str).substr(str.length)
23 }

vue文件引入

js文件用的是export,  vue文件引入的時候需要加{}

 

箭頭對應的就是上面{}里面的

template中這樣使用::


免責聲明!

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



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