Sfoglia il codice sorgente

feat(utils): update time formatting to use user's local timezone

- Changed the default timezone from Japan (Asia/Tokyo) to the user's current timezone for time and date formatting functions.
- Updated the `formatTime`, `formatDate`, and `dayjsTZ` functions to reflect this change, enhancing the utility's flexibility and user experience.
yb 5 giorni fa
parent
commit
7981af21c3
1 ha cambiato i file con 7 aggiunte e 7 eliminazioni
  1. 7 7
      src/utils/dayjs.ts

+ 7 - 7
src/utils/dayjs.ts

@@ -6,22 +6,22 @@ import timezone from 'dayjs/plugin/timezone'
 dayjs.extend(utc)
 dayjs.extend(timezone)
 
-// 设置默认时区为日本
-const DEFAULT_TIMEZONE = 'Asia/Tokyo'
+// 获取用户当前时区(浏览器本地时区)
+const getUserTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone
 
 /**
- * 格式化时间(使用日本时区)
+ * 格式化时间(使用用户当前时区)
  * @param time 时间字符串
  * @param format 格式化模板,默认 'YYYY-MM-DD HH:mm:ss'
  * @returns 格式化后的时间字符串
  */
 export function formatTime(time: string | undefined | null, format = 'YYYY-MM-DD HH:mm:ss'): string {
   if (!time) return '-'
-  return dayjs(time).tz(DEFAULT_TIMEZONE).format(format)
+  return dayjs(time).tz(getUserTimezone()).format(format)
 }
 
 /**
- * 格式化日期(使用日本时区)
+ * 格式化日期(使用用户当前时区)
  * @param time 时间字符串
  * @returns 格式化后的日期字符串 (YYYY-MM-DD)
  */
@@ -30,12 +30,12 @@ export function formatDate(time: string | undefined | null): string {
 }
 
 /**
- * 获取配置了时区的 dayjs 实例
+ * 获取配置了时区的 dayjs 实例(使用用户当前时区)
  * @param time 时间字符串
  * @returns dayjs 实例
  */
 export function dayjsTZ(time?: string | Date) {
-  return dayjs(time).tz(DEFAULT_TIMEZONE)
+  return dayjs(time).tz(getUserTimezone())
 }
 
 export { dayjs }