From 2b1b49dbe79e0b6a72882466ec89d62d93a5e9f6 Mon Sep 17 00:00:00 2001 From: monkeycf <2548772327@qq.com> Date: Wed, 2 Oct 2019 21:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9parseTime=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=9A=20=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=E4=B8=BAstring?= =?UTF-8?q?|null=EF=BC=8C=E6=AD=A3=E5=88=99=E5=8C=B9=E9=85=8D=EF=BC=8C?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E4=BD=8D=E6=95=B0=E4=B8=8D=E8=B6=B3=E8=A1=A5?= =?UTF-8?q?=E9=9B=B6=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index ffb55260..2684e3c2 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -6,7 +6,7 @@ * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat - * @returns {string} + * @returns {string | null} */ export function parseTime(time, cFormat) { if (arguments.length === 0) { @@ -34,14 +34,11 @@ export function parseTime(time, cFormat) { 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] + const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { + const 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 value.toString().padStart(2, '0') }) return time_str }