vue-cli 利用moment.js轉化時間格式為YYYY年MM月DD日,或者是YYYY-MM-DD HH:MM:SS 等格式


1.在mian.js引入moment

import moment from 'moment'
Vue.prototype.$moment = 'moment'

2. 在main.js 設置全局過濾器

Vue.filter('moment', function (value, formatString) {
  formatString = formatString || 'YYYY年MM月DD日'; // return moment(value).format(formatString); // value可以是普通日期 20170723
  return moment.unix(value).format(formatString); // 這是時間戳轉時間
});

//標紅處為格式的自定義 同樣可以YYYY-MM-DD HH:MM:SS ,或者 YYYY/MM/DD

 

3.渲染到頁面

 <div>{{time | moment}}</div>

 

 方法二:

 當然在某些特殊業務下,上面那種方法竟然沒渲染出來,可以試試下面這種:

var moment = require('moment') 引入頁面某一組件內
this.visitorTime = moment(val).format('YYYY-MM-DD HH:mm:ss') ;
這樣就可以了。


 

 

 
         
         
       


免責聲明!

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



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