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