vue使用moment.js格式化时间

npm install moment --save

然后在main.js引入

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

接着就可以在组件里面使用了

当前时间
moment().format('YYYY-MM-DD HH:mm:ss'); 
7天前的日期:
moment().subtract('days',7).format('YYYY年MM月DD日'); 
7天后的日期:
moment().add('days',7).format('YYYY年MM月DD日'); 
9小时前的时间:
moment().subtract('hours',9).format('HH:mm:ss'); 
9小时后的时间:
moment().add('hours',9).format('HH:mm:ss'); 
时间相减:
let time1= this.moment('2019-05-01 15:32:20');
let time2 = this.moment('2019-05-20 13:28:10');
console.log(time2.diff(time1, 'day'));//相差多少天
console.log(time2.diff(time1, 'minute'));//相差多少分钟
花括号里使用'$':
{{$moment(scope.row.time).format("MM-DD")}}

你可能感兴趣的:(vue)