From 500078714411487af00161e01bd7e0b5efdc3414 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期四, 07 八月 2025 13:32:32 +0800
Subject: [PATCH] add:热门景点
---
utils/time.ts | 29 +++++++++++++++++++++++++++++
1 files changed, 29 insertions(+), 0 deletions(-)
diff --git a/utils/time.ts b/utils/time.ts
index 8affc77..731a4a9 100644
--- a/utils/time.ts
+++ b/utils/time.ts
@@ -1,6 +1,35 @@
// utils/time.ts
export function formatRelativeTime(inputTime: string | Date): string {
+ const now = new Date()
+
+ // 如果是字符串格式,先进行兼容性处理
+ let time: Date
+ if (typeof inputTime === 'string') {
+ // 替换中间的空格为 'T' 或替换 '-' 为 '/'
+ const compatibleStr = inputTime.replace(/-/g, '/')
+ time = new Date(compatibleStr)
+ } else {
+ time = 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 '刚刚'
+}
+
+export function formatRelativeTime2(inputTime: string | Date): string {
const now = new Date()
const time = typeof inputTime === 'string' ? new Date(inputTime) : inputTime
const diffMs = now.getTime() - time.getTime()
--
Gitblit v1.9.3