Bladeren bron

fix: 修改时间格式

ansoni 5 maanden geleden
bovenliggende
commit
022540025e
2 gewijzigde bestanden met toevoegingen van 21 en 27 verwijderingen
  1. 5 5
      src/app/[locale]/(navbar)/cashback/@cashbackInfo/components/Extract.tsx
  2. 16 22
      src/utils/methods/index.ts

+ 5 - 5
src/app/[locale]/(navbar)/cashback/@cashbackInfo/components/Extract.tsx

@@ -1,7 +1,7 @@
 "use client";
 import { CashbackTypes } from "@/api/cashback";
 import { server } from "@/utils/client";
-import { timeFormat } from "@/utils/methods";
+import { brTimeFormat } from "@/utils/methods";
 import { Toast } from "antd-mobile";
 import { useAnimate } from "framer-motion";
 import { useTranslations } from "next-intl";
@@ -50,8 +50,8 @@ const Extract = (props: Props) => {
                     <div className={`${styles.timelineMarker} before:bg-[#fbc110]`}></div>
                     <div className={styles.timelineContent}>
                         <span className={"text-[0.12rem] text-[#6c6c6c]"}>
-                            {timeFormat(cashbackInfo.last_period.start_time, local)} ~{" "}
-                            {timeFormat(cashbackInfo.last_period.end_time, local)}
+                            {brTimeFormat(cashbackInfo.last_period.start_time)} ~{" "}
+                            {brTimeFormat(cashbackInfo.last_period.end_time)}
                         </span>
                     </div>
                 </li>
@@ -60,8 +60,8 @@ const Extract = (props: Props) => {
                     <div className={`${styles.timelineMarker} before:bg-[#9ae0b9]`}></div>
                     <div>
                         <span className={"text-[0.12rem] text-[#6c6c6c]"} ref={scope}>
-                            {timeFormat(cashbackInfo.next_period.start_time, local)} ~{" "}
-                            {timeFormat(cashbackInfo.next_period.end_time, local)}
+                            {brTimeFormat(cashbackInfo.next_period.start_time)} ~{" "}
+                            {brTimeFormat(cashbackInfo.next_period.end_time)}
                         </span>
                     </div>
                 </li>

+ 16 - 22
src/utils/methods/index.ts

@@ -5,6 +5,7 @@
  * @param immediate 是否立即执行
  * @returns 防抖后的函数
  */
+import dayjs from "dayjs";
 export function debounce<T extends (...args: any[]) => any>(
     fn: T,
     delay = 200,
@@ -68,33 +69,26 @@ export const timeFormat = (
     if (len === 10) {
         time = time * 1000;
     }
-    lang = getLang(lang);
 
-    const timeOffset = (new Date().getTimezoneOffset() - 3 * 60) * 60 * 1000;
-    const brTime = time + timeOffset;
     if (delimiter) {
-        return new Date(brTime)
-            .toLocaleString(lang, {
-                hour12: false,
-            })
-            .replaceAll("/", delimiter);
+        return dayjs(time).format("DD/MM/YYYY").replaceAll("/", delimiter);
     }
+    // 月 /日 /  年
+    // 日 / 月 / 年
     if (day) {
-        return new Date(brTime).toLocaleString(lang, {
-            year: "2-digit",
-            month: "2-digit",
-            day: "2-digit",
-            hour12: false,
-        });
+        return dayjs(time).format("DD/MM/YYYY");
     }
-    return new Date(brTime).toLocaleString(lang, {
-        year: "2-digit",
-        month: "2-digit",
-        day: "2-digit",
-        hour: "2-digit",
-        minute: "2-digit",
-        hour12: false,
-    });
+    return dayjs(time).format("DD/MM/YYYY HH:mm:ss");
+};
+export const brTimeFormat = (time: number) => {
+    if (!time) return "";
+    const len = (time + "").length;
+    if (len === 10) {
+        time = time * 1000;
+    }
+    const timeOffset = (new Date().getTimezoneOffset() - 3 * 60) * 60 * 1000;
+    const brTime = time + timeOffset;
+    return dayjs(brTime).format("DD/MM/YYYY HH:mm:ss");
 };
 
 /**