使用moment ,查看文档地址 http://momentjs.cn/
1)下载moment npm i moment
2)有些地方需要用到时间格式化 ,需要处理数据
我比较喜欢的做法有两种
方法有两种:
方法1.对定义的currentTime时间格式化
function formatTime(arr) {
arr.forEach(item => {
item.currentTime = moment(item.currentTime).format("YYYY年MM月DD日");
});
return arr;
}
getAllUsers(params).then((res) => { //这个方法是接口数据
// console.log(res);
// 对数组中时间格式化
this.tableData = formatTime(res.data.users);
this.total = res.data.total; //一共多少条数据
});
方法2:封装成过滤器 在main.js引入
import Vue rom "vue";
import moment from "moment";
Vue.filter("formatTime", function(time) {
return moment(time).format("YYYY-MM-DD");
});
在页面上应用
<template slot-scope="scope">{{scope.row.currentTime | formatTime}}</template>