feat: increase support for multiple time zones

This commit is contained in:
zhongming4762
2025-10-22 19:52:01 +08:00
parent 03ce030e7c
commit 0a8339a405
32 changed files with 577 additions and 9 deletions

View File

@@ -1,4 +1,9 @@
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
dayjs.extend(timezone);
export function formatDate(time: number | string, format = 'YYYY-MM-DD') {
try {
@@ -6,7 +11,7 @@ export function formatDate(time: number | string, format = 'YYYY-MM-DD') {
if (!date.isValid()) {
throw new Error('Invalid date');
}
return date.format(format);
return date.tz().format(format);
} catch (error) {
console.error(`Error formatting date: ${error}`);
return time;
@@ -24,3 +29,19 @@ export function isDate(value: any): value is Date {
export function isDayjsObject(value: any): value is dayjs.Dayjs {
return dayjs.isDayjs(value);
}
/**
* 设置默认时区
* @param timezone
*/
export const setDefaultTimezone = (timezone?: string) => {
timezone ? dayjs.tz.setDefault(timezone) : dayjs.tz.setDefault();
};
/**
* 获取当前时区
* @returns 当前时区
*/
export const getTimezone = () => {
return dayjs.tz.guess();
};

View File

@@ -3,4 +3,5 @@ export type * from './basic';
export type * from './helper';
export type * from './menu-record';
export type * from './tabs';
export type * from './user-profile';
export type * from './vue-router';

View File

@@ -0,0 +1,9 @@
/**
* 时区选项
*/
interface TimezoneOption {
offset: number;
timezone: string;
}
export type { TimezoneOption };