MCPcopy Create free account
hub / github.com/experdot/pointer / formatRelativeTime

Function formatRelativeTime

src/renderer/src/utils/timeFormatter.ts:14–37  ·  view source on GitHub ↗
(timestamp: number)

Source from the content-addressed store, hash-verified

12 * @returns 相对时间字符串
13 */
14export const formatRelativeTime = (timestamp: number): string => {
15 const now = dayjs()
16 const target = dayjs(timestamp)
17
18 // 检查是否是今天
19 if (now.isSame(target, 'day')) {
20 // 当天显示具体时间(精确到秒)
21 return target.format('HH:mm:ss')
22 }
23
24 // 检查是否是昨天
25 if (now.subtract(1, 'day').isSame(target, 'day')) {
26 return `昨天 ${target.format('HH:mm')}`
27 }
28
29 // 检查是否是今年
30 if (now.isSame(target, 'year')) {
31 // 同年显示月日和时间
32 return target.format('MM月DD日 HH:mm')
33 }
34
35 // 不同年份显示完整日期时间
36 return target.format('YYYY年MM月DD日 HH:mm')
37}
38
39/**
40 * 格式化时间戳为具体日期时间(用于tooltip)

Callers 1

RelativeTimeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected