1.安装依赖
npm install moment --save
2据需求我这里放在html中根据拿到的时间调用时间方法
{this.showTime(scope.row.statusTime)}
3.引入
import moment from "moment";
data(){
return{
nowDate: moment(),
}
}
//4.进入页面就调用一次,设置异步回调setInterval,每隔一秒调用一次
mounted () {
setInterval(() => {
this.nowDate = moment();
}, 1000);
},
//5.方法
showTime (date) {
if (date == null) {
return "";
}
let nowTime = this.nowDate; //当下时间
let setTime = moment(date);
let times = moment.duration(nowTime - setTime, "ms"); //做差
let days = times.get("days");
let hours = times.get("hours");
let mins = times.get("minutes");
let ss = times.get("seconds");
return `${days}天${hours}小时${mins}分${ss}秒`;
},