huangxw преди 1 месец
родител
ревизия
3603f213ef
променени са 1 файла, в които са добавени 30 реда и са изтрити 12 реда
  1. 30 12
      src/utils/ruoyi.ts

+ 30 - 12
src/utils/ruoyi.ts

@@ -295,18 +295,36 @@ export const dataURLtoBlob = (base64Buf: string): Blob => {
 };
 // 传入一个时间2025-11-21 16:56:10,返回今天昨天前天,如果不是返回2022-09-09格式根据日期
 export const formatDateRelative = (date: Date): string => {
-    const now = new Date();
-    const inputDate = new Date(date);
-    const diffTime = now.getTime() - inputDate.getTime();
-    const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
+    try {
+        // 创建日期对象
+        const inputDate = new Date(date);
+        const now = new Date();
 
-    if (diffDays === 0) {
-        return '今天';
-    } else if (diffDays === 1) {
-        return '昨天';
-    } else if (diffDays === 2) {
-        return '前天';
-    } else {
-        return parseTime(inputDate, '{y}-{m}-{d}') as string;
+        // 只比较日期部分
+        const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
+        const inputDay = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
+
+        // 计算天数差
+        const timeDiff = today.getTime() - inputDay.getTime();
+        const dayDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
+        switch (dayDiff) {
+            case 0:
+                return '今天';
+            case 1:
+                return '昨天';
+            case 2:
+                return '2天前';
+            default:
+                if (dayDiff > 0) {
+                    return `${dayDiff}天前`;
+                } else if (dayDiff === -1) {
+                    return '明天';
+                } else {
+                    return `${Math.abs(dayDiff)}天后`;
+                }
+        }
+    } catch (e) {
+        console.error('日期解析错误:', e);
+        return '日期格式错误';
     }
 };