2023-06-08 14:39:53 +00:00
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
|
2023-06-08 15:19:32 +00:00
|
|
|
|
// 读取本地存储中的语言配置
|
|
|
|
|
import { lang } from '@/utils/localStorageLang'
|
|
|
|
|
|
|
|
|
|
const language = lang === 'en' ? true : false
|
|
|
|
|
|
|
|
|
|
/* 这里老问题不可以用 vue-i18n 的 t 函数,因为不在 setup 里面
|
|
|
|
|
mins, years 之类的就不用了,有点丑 */
|
|
|
|
|
|
|
|
|
|
export const formatTime = (time: number) => {
|
2023-06-08 14:39:53 +00:00
|
|
|
|
const publishTime = dayjs(time)
|
|
|
|
|
const now = dayjs()
|
|
|
|
|
const diffInSeconds = now.diff(publishTime, 'second')
|
|
|
|
|
|
|
|
|
|
if (diffInSeconds < 60) {
|
2023-06-08 15:19:32 +00:00
|
|
|
|
return language ? `${diffInSeconds}s ago` : `${diffInSeconds}秒前`
|
2023-06-08 14:39:53 +00:00
|
|
|
|
} else if (diffInSeconds < 3600) {
|
|
|
|
|
const diffInMinutes = Math.floor(diffInSeconds / 60)
|
2023-06-08 15:19:32 +00:00
|
|
|
|
return language ? `${diffInMinutes}min ago` : `${diffInMinutes}分钟前`
|
2023-06-08 14:39:53 +00:00
|
|
|
|
} else if (diffInSeconds < 86400) {
|
|
|
|
|
const diffInHours = Math.floor(diffInSeconds / 3600)
|
2023-06-08 15:19:32 +00:00
|
|
|
|
return language ? `${diffInHours}h ago` : `${diffInHours}时前`
|
2023-06-08 14:39:53 +00:00
|
|
|
|
} else if (diffInSeconds < 2592000) {
|
|
|
|
|
const diffInDays = Math.floor(diffInSeconds / 86400)
|
2023-06-08 15:19:32 +00:00
|
|
|
|
return language ? `${diffInDays}day ago` : `${diffInDays}天前`
|
|
|
|
|
} else if (diffInSeconds < 31536000) {
|
2023-06-08 14:39:53 +00:00
|
|
|
|
const diffInMonths = Math.floor(diffInSeconds / 2592000)
|
2023-06-08 15:19:32 +00:00
|
|
|
|
return language ? `${diffInMonths}month ago` : `${diffInMonths}月前`
|
|
|
|
|
} else {
|
|
|
|
|
const diffInMonths = Math.floor(diffInSeconds / 31536000)
|
|
|
|
|
return language ? `${diffInMonths}year ago` : `${diffInMonths}年前`
|
2023-06-08 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|