diff --git a/package.json b/package.json index 15bca65..af2fd22 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "vitepress": "^1.6.3" }, "dependencies": { - "dayjs": "^1.10.6", + "dayjs": "^1.11.13", "idb": "^8.0.0", "js-base64": "^3.7.7", "js-cookie": "^3.0.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 727b9fa..2b7ad72 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,7 +13,7 @@ specifiers: commitlint: ^19.5.0 conventional-changelog-cli: ^2.0.31 cz-conventional-changelog: ^3.3.0 - dayjs: ^1.10.6 + dayjs: ^1.11.13 fake-indexeddb: ^4 gh-pages: ^6.3.0 husky: ^9.1.6 diff --git a/src/formatDateTime/index.ts b/src/formatDateTime/index.ts index cad6c60..2d7f12f 100644 --- a/src/formatDateTime/index.ts +++ b/src/formatDateTime/index.ts @@ -1,4 +1,18 @@ -import dayjs from 'dayjs'; +import baseDayJs from 'dayjs'; +import timezone from 'dayjs/plugin/timezone'; // ES 2015 +import utc from 'dayjs/plugin/utc'; // ES 2015 + +baseDayJs.extend(utc); +baseDayJs.extend(timezone); +// 默认时区 +const DEFAULT_TIMEZONE = 'Asia/Shanghai'; +/** + * 将时间转换为默认时区 + * @param {string | number | Date} time - 输入时间 + * @return {dayjs.Dayjs} 带时区的Dayjs对象 + */ +const dayjs = (...args) => baseDayJs(...args).tz(DEFAULT_TIMEZONE); + /** * @category 枚举 * 日期和时间格式模式的枚举 @@ -74,7 +88,7 @@ export enum DateTimeFormat { FULL_DATETIME_ISO = 'YYYY-MM-DDTHH:mm:ssZ', } -type DateTimeInput = string | number | Date | dayjs.Dayjs; +type DateTimeInput = string | number | Date | baseDayJs.Dayjs; type FormatPattern = DateTimeFormat | string; /** * 一个日期时间格式化工具,可处理各种输入类型和格式化模式。 @@ -120,7 +134,7 @@ type FormatPattern = DateTimeFormat | string; export const formatDateTime = ( date: DateTimeInput, format: FormatPattern = DateTimeFormat.STANDARD -): string | dayjs.Dayjs => { +): string | baseDayJs.Dayjs => { const isValidFormat = Object.values(DateTimeFormat).includes(format); if (!isValidFormat) {