kun-galgame-vue/src/utils/formatTime.ts

35 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-06-08 14:39:53 +00:00
import dayjs from 'dayjs'
2023-06-08 15:19:32 +00:00
// 读取本地存储中的语言配置
2023-08-20 13:38:40 +00:00
import { KUNGalgameLanguage } from '@/utils/getDefaultEnv'
2023-06-08 15:19:32 +00:00
2023-08-20 13:38:40 +00:00
const language = KUNGalgameLanguage === 'en' ? true : false
2023-06-08 15:19:32 +00:00
/* 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
}
}