<template> <view class=""> <view v-for="item in contList">{{fmtTime(item.timer)}}</view> </view> </template> <script> export default { data() { return { contList:[ {timer:"2020-11-03 8:10:59"}, {timer:"2020-11-03 3:10:59"}, {timer:"2020-11-01 8:10:59"}, ] } }, methods: { addZero(num) { num = num.toString(); return num[1] ? num : "0" + num; }, fmtTime(str) { let now = new Date(); let thatTime = new Date(str); // let totalSec = math.floor ((nowthattime) / 1000); If (totalSec < 60) return "just "; Else if (totalSec < 60 * 60) return math. floor(totalSec / 60) + "min ago "; Elseif (totalSec < 60 * 60 * 24) return math. floor(totalSec / 60/60) + "before" elseIf (totalSec / 60/60) + "before" elseIf (totalSec / 60/60) + "before" [thatTime.getFullYear(), this.addZero(thatTime.getMonth() + 1), this.addZero(thatTime.getDate()) ].join("-") + " " + [this.addZero(thatTime.getHours()), this.addZero(thatTime.getMinutes()), this.addZero(thatTime.getSeconds())] .join(":"); } } } </script> <style> </style>Copy the code