feat(utils): moment换成dayjs

This commit is contained in:
roymondchen 2022-08-08 18:53:43 +08:00 committed by jia000
parent 5f78bbd7b7
commit 9ac3e12487
4 changed files with 19 additions and 25 deletions

View File

@ -38,7 +38,6 @@
"@tmagic/utils": "1.1.0-beta.6",
"element-plus": "^2.2.6",
"lodash-es": "^4.17.21",
"moment": "^2.29.2",
"sortablejs": "^1.14.0",
"vue": "^3.2.37"
},

View File

@ -25,12 +25,12 @@
},
"dependencies": {
"@tmagic/schema": "1.1.0-beta.6",
"moment": "^2.29.2"
"dayjs": "^1.11.4"
},
"devDependencies": {
"@types/node": "^15.12.4",
"typescript": "^4.7.4",
"rimraf": "^3.0.2",
"typescript": "^4.7.4",
"vite": "^3.0.4"
}
}

View File

@ -16,13 +16,16 @@
* limitations under the License.
*/
import moment from 'moment';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import type { MNode } from '@tmagic/schema';
import { NodeType } from '@tmagic/schema';
export * from './dom';
dayjs.extend(utc);
export const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => {
const timer = setTimeout(() => {
@ -31,26 +34,20 @@ export const sleep = (ms: number): Promise<void> =>
}, ms);
});
export const datetimeFormatter = (v: string | Date, defaultValue = '-', f = 'YYYY-MM-DD HH:mm:ss'): any => {
let format = f;
if (format === 'timestamp') {
format = 'x';
}
export const datetimeFormatter = (v: string | Date, defaultValue = '-', format = 'YYYY-MM-DD HH:mm:ss'): any => {
if (v) {
let time = null;
if ((typeof v === 'string' && v.includes('Z')) || v.constructor === Date) {
if (['x', 'timestamp'].includes(format)) {
time = dayjs(v).valueOf();
} else if ((typeof v === 'string' && v.includes('Z')) || v.constructor === Date) {
// UTC字符串时间或Date对象格式化为北京时间
time = moment(v).utcOffset('+08:00').format(format);
time = dayjs(v).utcOffset(8).format(format);
} else {
time = moment(v).format(format);
time = dayjs(v).format(format);
}
if (format === 'x') {
return +time;
}
// 格式化为北京时间
if (time !== 'Invalid date') {
if (time !== 'Invalid Date') {
return time;
}
return defaultValue;

14
pnpm-lock.yaml generated
View File

@ -227,7 +227,6 @@ importers:
'@vue/test-utils': ^2.0.0
element-plus: ^2.2.6
lodash-es: ^4.17.21
moment: ^2.29.2
rimraf: ^3.0.2
sass: ^1.35.1
sortablejs: ^1.14.0
@ -240,7 +239,6 @@ importers:
'@tmagic/utils': link:../utils
element-plus: 2.2.6_vue@3.2.37
lodash-es: 4.17.21
moment: 2.29.3
sortablejs: 1.15.0
vue: 3.2.37
devDependencies:
@ -414,13 +412,13 @@ importers:
specifiers:
'@tmagic/schema': 1.1.0-beta.6
'@types/node': ^15.12.4
moment: ^2.29.2
dayjs: ^1.11.4
rimraf: ^3.0.2
typescript: ^4.7.4
vite: ^3.0.4
dependencies:
'@tmagic/schema': link:../schema
moment: 2.29.3
dayjs: 1.11.4
devDependencies:
'@types/node': 15.14.9
rimraf: 3.0.2
@ -3153,6 +3151,10 @@ packages:
resolution: {integrity: sha512-xxwlswWOlGhzgQ4TKzASQkUhqERI3egRNqgV4ScR8wlANA/A9tZ7miXa44vTTKEq5l7vWoL5G57bG3zA+Kow0A==}
dev: false
/dayjs/1.11.4:
resolution: {integrity: sha512-Zj/lPM5hOvQ1Bf7uAvewDaUcsJoI6JmNqmHhHl3nyumwe0XHwt8sWdOVAPACJzCebL8gQCi+K49w7iKWnGwX9g==}
dev: false
/de-indent/1.0.2:
resolution: {integrity: sha512-e/1zu3xH5MQryN2zdVaF0OrdNLUbvWxzMbi+iNA6Bky7l1RoP8a2fIbRocyHclXt/arDrrR6lL3TqFD9pMQTsg==}
dev: true
@ -5264,10 +5266,6 @@ packages:
engines: {node: '>=0.10.0'}
dev: true
/moment/2.29.3:
resolution: {integrity: sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==}
dev: false
/monaco-editor/0.32.1:
resolution: {integrity: sha512-LUt2wsUvQmEi2tfTOK+tjAPvt7eQ+K5C4rZPr6SeuyzjAuAHrIvlUloTcOiGjZW3fn3a/jFQCONrEJbNOaCqbA==}
dev: false