Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
35b1bc37
Commit
35b1bc37
authored
Dec 27, 2025
by
IanShaw027
Browse files
refactor(frontend): 优化格式化工具函数
- 改进数据格式化逻辑 - 增强工具函数可读性
parent
8d387886
Changes
1
Hide whitespace changes
Inline
Side-by-side
frontend/src/utils/format.ts
View file @
35b1bc37
...
@@ -3,30 +3,32 @@
...
@@ -3,30 +3,32 @@
* 参考 CRS 项目的 format.js 实现
* 参考 CRS 项目的 format.js 实现
*/
*/
import
{
i18n
}
from
'
@/i18n
'
/**
/**
* 格式化相对时间
* 格式化相对时间
* @param date 日期字符串或 Date 对象
* @param date 日期字符串或 Date 对象
* @returns 相对时间字符串,如 "5m ago", "2h ago", "3d ago"
* @returns 相对时间字符串,如 "5m ago", "2h ago", "3d ago"
*/
*/
export
function
formatRelativeTime
(
date
:
string
|
Date
|
null
|
undefined
):
string
{
export
function
formatRelativeTime
(
date
:
string
|
Date
|
null
|
undefined
):
string
{
if
(
!
date
)
return
'
N
ever
'
if
(
!
date
)
return
i18n
.
global
.
t
(
'
common.time.n
ever
'
)
const
now
=
new
Date
()
const
now
=
new
Date
()
const
past
=
new
Date
(
date
)
const
past
=
new
Date
(
date
)
const
diffMs
=
now
.
getTime
()
-
past
.
getTime
()
const
diffMs
=
now
.
getTime
()
-
past
.
getTime
()
// 处理未来时间或无效日期
// 处理未来时间或无效日期
if
(
diffMs
<
0
||
isNaN
(
diffMs
))
return
'
N
ever
'
if
(
diffMs
<
0
||
isNaN
(
diffMs
))
return
i18n
.
global
.
t
(
'
common.time.n
ever
'
)
const
diffSecs
=
Math
.
floor
(
diffMs
/
1000
)
const
diffSecs
=
Math
.
floor
(
diffMs
/
1000
)
const
diffMins
=
Math
.
floor
(
diffSecs
/
60
)
const
diffMins
=
Math
.
floor
(
diffSecs
/
60
)
const
diffHours
=
Math
.
floor
(
diffMins
/
60
)
const
diffHours
=
Math
.
floor
(
diffMins
/
60
)
const
diffDays
=
Math
.
floor
(
diffHours
/
24
)
const
diffDays
=
Math
.
floor
(
diffHours
/
24
)
if
(
diffDays
>
0
)
return
`
${
diffDays
}
d ago`
if
(
diffDays
>
0
)
return
i18n
.
global
.
t
(
'
common.time.daysAgo
'
,
{
n
:
diffDays
})
if
(
diffHours
>
0
)
return
`
${
diffHours
}
h ago`
if
(
diffHours
>
0
)
return
i18n
.
global
.
t
(
'
common.time.hoursAgo
'
,
{
n
:
diffHours
})
if
(
diffMins
>
0
)
return
`
${
diffMins
}
m ago`
if
(
diffMins
>
0
)
return
i18n
.
global
.
t
(
'
common.time.minutesAgo
'
,
{
n
:
diffMins
})
return
'
J
ust
n
ow
'
return
i18n
.
global
.
t
(
'
common.time.j
ust
N
ow
'
)
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment