From f5f314e167906ec5b15d4f56568408982178caa1 Mon Sep 17 00:00:00 2001 From: tj <1378534974@qq.com> Date: 星期一, 26 五月 2025 17:00:16 +0800 Subject: [PATCH] 内容精选,光影社区接口对接 --- utils/time.ts | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) diff --git a/utils/time.ts b/utils/time.ts new file mode 100644 index 0000000..8affc77 --- /dev/null +++ b/utils/time.ts @@ -0,0 +1,21 @@ +// utils/time.ts + +export function formatRelativeTime(inputTime: string | Date): string { + const now = new Date() + const time = typeof inputTime === 'string' ? new Date(inputTime) : inputTime + const diffMs = now.getTime() - time.getTime() + + const minutes = Math.floor(diffMs / (1000 * 60)) + const hours = Math.floor(minutes / 60) + const days = Math.floor(hours / 24) + const months = Math.floor(days / 30) + const years = Math.floor(days / 365) + + if (years > 0) return `${years} 年前` + if (months > 0) return `${months} 个月前` + if (days > 0) return `${days} 天前` + if (hours > 0) return `${hours} 小时前` + if (minutes > 0) return `${minutes} 分钟前` + return '刚刚' + } + \ No newline at end of file -- Gitblit v1.9.3