Skip to content

perf(upgrade dayjs's version): upgrade dayjs's version and set default timezone for dayjs #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci 文件的变更考虑不在当前 PR 体现

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单独提了一个PR
#104


- name: Cache yarn.lock
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: package-temp-dir
key: lock-${{ github.sha }}
Expand All @@ -34,7 +34,7 @@ jobs:
cp yarn.lock package-temp-dir
- name: Cache node_modules
id: node_modules_cache_id
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
Expand All @@ -50,13 +50,13 @@ jobs:
- uses: actions/checkout@v2

- name: Restore cache from yarn.lock
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: Restore cache from node_modules
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
Expand All @@ -71,13 +71,13 @@ jobs:
- uses: actions/checkout@v2

- name: Restore cache from yarn.lock
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: Restore cache from node_modules
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
Expand All @@ -92,13 +92,13 @@ jobs:
- uses: actions/checkout@v2

- name: Restore cache from yarn.lock
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: Restore cache from node_modules
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
Expand All @@ -118,13 +118,13 @@ jobs:
- uses: actions/checkout@v2

- name: Restore cache from yarn.lock
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: Restore cache from node_modules
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"vue-cli-plugin-commitlint": "^1.0.4"
},
"dependencies": {
"dayjs": "^1.10.6",
"dayjs": "^1.11.13",
"lodash": "^4.17.21",
"standard-version": "^9.3.1"
},
Expand Down
25 changes: 19 additions & 6 deletions src/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
*/

import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone'; // ES 2015
import utc from 'dayjs/plugin/utc'; // ES 2015

dayjs.extend(utc);
dayjs.extend(timezone);
// 默认时区
const DEFAULT_TIMEZONE = 'Asia/Shanghai';
/**
* 将时间转换为默认时区
* @param {string | number | Date} time - 输入时间
* @return {dayjs.Dayjs} 带时区的Dayjs对象
*/
const toLocalTime = (...args) => dayjs(...args).tz(DEFAULT_TIMEZONE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样是否可以呢

import dayjs as baseDayjs from 'dayjs'

export const dayjs = (...args, timezone = DEFAULT_TIMEZONE) => baseDayjs(...args).tz(timezone)

// 当前文件提供的方法
formatDateTime(timeData: string | number | Date) {
    return dayjs(timeData).format('YYYY-MM-DD HH:mm:ss');
}

// 子产品使用
import dayjs from 'dt-utils/dateTime'

dayjs(time).format('HH:mm')


/**
* 时间处理
Expand All @@ -15,47 +28,47 @@ const dateTime = {
* @return {string}
*/
formatDateTime(timeData: string | number | Date) {
return dayjs(timeData).format('YYYY-MM-DD HH:mm:ss');
return toLocalTime(timeData).format('YYYY-MM-DD HH:mm:ss');
},
/**
* 返回 YYYY-MM-DD 格式化的字符串
* @param {string | number | Date} timeData
* @return {string}
*/
formatDate(timeData: string | number | Date) {
return dayjs(timeData).format('YYYY-MM-DD');
return toLocalTime(timeData).format('YYYY-MM-DD');
},
/**
* 返回 YYYY-MM-DD HH:mm 格式化的字符串
* @param {string | number | Date} timeData
* @return {string}
*/
formatDateHours(timeData: string | number | Date) {
return dayjs(timeData).format('YYYY-MM-DD HH:mm');
return toLocalTime(timeData).format('YYYY-MM-DD HH:mm');
},
/**
* 返回 MM-DD HH:mm 格式化的字符串
* @param {string | number | Date} timeData
* @return {string}
*/
formatDayHours(timeData: string | number | Date) {
return dayjs(timeData).format('MM-DD HH:mm');
return toLocalTime(timeData).format('MM-DD HH:mm');
},
/**
* 返回 HH:mm 格式化的字符串
* @param {string | number | Date} timeData
* @return {string}
*/
formatHours(timeData: string | number | Date) {
return dayjs(timeData).format('HH:mm');
return toLocalTime(timeData).format('HH:mm');
},
/**
* 返回 HH:mm:ss 格式化的字符串
* @param {string | number | Date} timeData
* @return {string}
*/
formatMinute(timeData: string | number | Date) {
return dayjs(timeData).format('HH:mm:ss');
return toLocalTime(timeData).format('HH:mm:ss');
},
/**
* 把秒转换成 HH[h]mm[m]ss[s] 的格式
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3573,10 +3573,10 @@ dateformat@^3.0.0:
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==

dayjs@^1.10.6:
version "1.11.5"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.5.tgz#00e8cc627f231f9499c19b38af49f56dc0ac5e93"
integrity sha512-CAdX5Q3YW3Gclyo5Vpqkgpj8fSdLQcRuzfX6mC6Phy0nfJ0eGYOeS7m4mt2plDWLAtA4TqTakvbboHvUxfe4iA==
dayjs@^1.11.13:
version "1.11.13"
resolved "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.13.tgz#92430b0139055c3ebb60150aa13e860a4b5a366c"
integrity sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==

debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.9:
version "2.6.9"
Expand Down