u
This commit is contained in:
1 parent
2efeb7cee2
commit
c0531c1d0a
35 files changed
+1817
-984
No files matched your search
@@ -73,43 +73,6 @@ export function isInMonth(date: string, month: string): boolean {
|
||||
return dayjs(date).format('YYYY-MM') === month
|
||||
}
|
||||
|
||||
export function formatRelativeTime(date: string | Date | number): string {
|
||||
if (typeof date === 'string' && !date.trim()) {
|
||||
return '暂无活跃'
|
||||
}
|
||||
|
||||
const d = dayjs(date)
|
||||
if (!d.isValid())
|
||||
return '暂无活跃'
|
||||
|
||||
const now = dayjs()
|
||||
const diff = now.diff(d, 'second') // 相差秒数
|
||||
|
||||
if (diff < 60) {
|
||||
return '刚刚'
|
||||
}
|
||||
else if (diff < 3600) {
|
||||
const minutes = Math.floor(diff / 60)
|
||||
return `${minutes}分钟前`
|
||||
}
|
||||
else if (diff < 86400) {
|
||||
const hours = Math.floor(diff / 3600)
|
||||
return `${hours}小时前`
|
||||
}
|
||||
else if (diff < 2592000) { // 30天以内
|
||||
const days = Math.floor(diff / 86400)
|
||||
return `${days}天前`
|
||||
}
|
||||
else if (diff < 31536000) { // 1年以内
|
||||
const months = Math.floor(diff / 2592000)
|
||||
return `${months}个月前`
|
||||
}
|
||||
else {
|
||||
const years = Math.floor(diff / 31536000)
|
||||
return `${years}年前`
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本月的第一天
|
||||
export function getStartOfMonth() {
|
||||
return dayjs().startOf('month').format('YYYY-MM-DD')
|
||||
@@ -150,3 +113,51 @@ export function normalizeDateTime(
|
||||
|
||||
return d.format('YYYY-MM-DD HH:mm:ss')
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间为相对时间
|
||||
* @param timestamp 秒级或毫秒级时间戳
|
||||
*/
|
||||
export function formatRelativeTime(timestamp: number): string {
|
||||
if (!timestamp)
|
||||
return '未活跃'
|
||||
|
||||
// 兼容秒级时间戳
|
||||
const time
|
||||
= timestamp.toString().length === 10
|
||||
? dayjs(timestamp * 1000)
|
||||
: dayjs(timestamp)
|
||||
|
||||
const now = dayjs()
|
||||
|
||||
const diffSeconds = now.diff(time, 'second')
|
||||
const diffMinutes = now.diff(time, 'minute')
|
||||
const diffHours = now.diff(time, 'hour')
|
||||
const diffDays = now.diff(time, 'day')
|
||||
|
||||
if (diffSeconds < 30) {
|
||||
return '刚刚'
|
||||
}
|
||||
|
||||
if (diffMinutes < 60) {
|
||||
return `${diffMinutes}分钟前`
|
||||
}
|
||||
|
||||
if (diffHours < 24) {
|
||||
return `${diffHours}小时前`
|
||||
}
|
||||
|
||||
if (diffDays === 1) {
|
||||
return '昨天'
|
||||
}
|
||||
|
||||
if (diffDays < 7) {
|
||||
return `${diffDays}天前`
|
||||
}
|
||||
|
||||
if (time.year() === now.year()) {
|
||||
return time.format('MM-DD HH:mm')
|
||||
}
|
||||
|
||||
return time.format('YYYY-MM-DD')
|
||||
}
|
||||
Reference in new issue
Block a user