From defc9db18896c4906a9fefff090a90a5b0f03380 Mon Sep 17 00:00:00 2001 From: fupeijie <1161353057@qq.com> Date: Wed, 27 Nov 2024 10:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds-console/console-ui/src/utils/ruoyi.js | 58 +++++++++++++---------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/rds-console/console-ui/src/utils/ruoyi.js b/rds-console/console-ui/src/utils/ruoyi.js index 1da753a..9e0b0f7 100644 --- a/rds-console/console-ui/src/utils/ruoyi.js +++ b/rds-console/console-ui/src/utils/ruoyi.js @@ -6,25 +6,34 @@ */ // 日期格式化 -export function parseTime(time, pattern) { - if (arguments.length === 0 || !time) { - return null - } - const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' - let date +export function parseTime(time, pattern = '{y}-{m}-{d} {h}:{i}:{s}') { + if (!time) return null; + + let date; + + // 判断 time 的类型并进行转换 if (typeof time === 'object') { - date = time + date = time; } else { - if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { - time = parseInt(time) - } else if (typeof time === 'string') { - time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), ''); + if (typeof time === 'string') { + if (/^\d+$/.test(time)) { + time = parseInt(time, 10); + } else { + time = time + .replace(/-/g, '/') + .replace('T', ' ') + .replace(/\.\d{3}/g, ''); + } } - if ((typeof time === 'number') && (time.toString().length === 10)) { - time = time * 1000 + if (typeof time === 'number' && time.toString().length === 10) { + time *= 1000; } - date = new Date(time) + date = new Date(time); } + + if (isNaN(date.getTime())) return null; // 无效日期校验 + + // 格式化时间 const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, @@ -32,20 +41,17 @@ export function parseTime(time, pattern) { h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), - a: date.getDay() - } - const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { - let value = formatObj[key] - // Note: getDay() returns 0 on Sunday - if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } - if (result.length > 0 && value < 10) { - value = '0' + value - } - return value || 0 - }) - return time_str + a: date.getDay(), + }; + + return pattern.replace(/{(y|m|d|h|i|s|a)+}/g, (match, key) => { + let value = formatObj[key]; + if (key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][value]; + return value < 10 && match.length > 0 ? `0${value}` : value; + }); } + // 表单重置 export function resetForm(refName) { if (this.$refs[refName]) { -- Gitee