1.安装依赖
npm install moment --save
2.main.js中引用
import moment from 'moment'
Vue.prototype.$moment = moment
3.在组件中使用 (使用prototype将moment绑定在vue实例的原型上调用的时候使用this.)
let time = new Date();
this.yearM = this.$moment(time).format('YYYY-MM-DD')
this.nowDate = this.$moment(time).format('HH:mm:ss')
format(写入需要的格式)
this.$moment('string').format("YYYY-DD-MM")
也可以设置过滤器
//在组件中引入
import moment from 'moment'//导入文件
moment.locale('zh-cn'); //汉化
filters:{
datefilter:function(value){
return moment(value).format('MMMM Do YYYY, h:mm:ss a');
}
},
<span>{{ time | datefilter }}</span><br>