import dayjs from 'dayjs'
export const getTime = (dateTimeStamp) => {
const before = new Date(dateTimeStamp).getTime()
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const month = day * 30;
const now = new Date().getTime();
const timeDiff = now - before;
if (before <= 0) return
if(timeDiff < 0) return
const monthBefore = timeDiff / month
const dayBefore =timeDiff / day
const hourBefore =timeDiff / hour
if (monthBefore > 1) {
return dayjs(dateTimeStamp).format('YYYY年MM月DD日')
}
else if(dayBefore>=1) {
return ""+ parseInt(dayBefore) +"天前"
}
else if(hourBefore>=1) {
return ""+ parseInt(hourBefore) +"小时前"
}
else
return "刚刚"
}
export const getSpecificTime = (val) => {
return dayjs(val).format('YYYY年MM月DD日 HH:mm')
}
import { getTime } from '@/services/time'
computed: {
timer() {
return getTime(this.data.dateTime)
},
},
<div> {{ timer }}</div>