Skip to content

Commit 54531eb

Browse files
author
zhaoge
committed
feat: upgrade dayjs's version and set default timezone for dayjs
1 parent 03e7339 commit 54531eb

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"vitepress": "^1.6.3"
7171
},
7272
"dependencies": {
73-
"dayjs": "^1.10.6",
73+
"dayjs": "^1.11.13",
7474
"idb": "^8.0.0",
7575
"js-base64": "^3.7.7",
7676
"js-cookie": "^3.0.5",

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/formatDateTime/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
import dayjs from 'dayjs';
1+
import baseDayJs from 'dayjs';
2+
import timezone from 'dayjs/plugin/timezone'; // ES 2015
3+
import utc from 'dayjs/plugin/utc'; // ES 2015
4+
5+
baseDayJs.extend(utc);
6+
baseDayJs.extend(timezone);
7+
// 默认时区
8+
const DEFAULT_TIMEZONE = 'Asia/Shanghai';
9+
/**
10+
* 将时间转换为默认时区
11+
* @param {string | number | Date} time - 输入时间
12+
* @return {dayjs.Dayjs} 带时区的Dayjs对象
13+
*/
14+
const dayjs = (...args) => baseDayJs(...args).tz(DEFAULT_TIMEZONE);
15+
216
/**
317
* @category 枚举
418
* 日期和时间格式模式的枚举
@@ -74,7 +88,7 @@ export enum DateTimeFormat {
7488
FULL_DATETIME_ISO = 'YYYY-MM-DDTHH:mm:ssZ',
7589
}
7690

77-
type DateTimeInput = string | number | Date | dayjs.Dayjs;
91+
type DateTimeInput = string | number | Date | baseDayJs.Dayjs;
7892
type FormatPattern = DateTimeFormat | string;
7993
/**
8094
* 一个日期时间格式化工具,可处理各种输入类型和格式化模式。
@@ -120,7 +134,7 @@ type FormatPattern = DateTimeFormat | string;
120134
export const formatDateTime = (
121135
date: DateTimeInput,
122136
format: FormatPattern = DateTimeFormat.STANDARD
123-
): string | dayjs.Dayjs => {
137+
): string | baseDayJs.Dayjs => {
124138
const isValidFormat = Object.values<string>(DateTimeFormat).includes(format);
125139

126140
if (!isValidFormat) {

0 commit comments

Comments
 (0)