From eda55e04f01fb3c5552482ef3ba0284a11e2979c Mon Sep 17 00:00:00 2001 From: ankio Date: Wed, 4 Sep 2024 00:29:19 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20(utils):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rhino不支持分组正则表达式 --- src/utils/Time.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/utils/Time.js b/src/utils/Time.js index f9fa72d..a4adf8a 100644 --- a/src/utils/Time.js +++ b/src/utils/Time.js @@ -27,21 +27,20 @@ export function formatDate(time = '', tpl = '') { if (time.length === 0) { return Math.floor(Date.now() / 1000) * 1000; } + const dateObj = {}; let tplRegex = tpl; if (!tpl.match(/[YMDhms]+/g)) { throw new Error('Invalid format string'); } - // /^(?\d{4})-(?\d{2})-(?\d{2}) (?\d{2}):(?\d{2}):(?\d{2})$/ - tplRegex = tplRegex - .replace(/Y/g, '(?\\d+)') - .replace(/M/g, '(?\\d+)') - .replace(/D/g, '(?\\d+)') - .replace(/h/g, '(?\\d+)') - .replace(/i/g, '(?\\d+)') - .replace(/s/g, '(?\\d+)'); + .replace(/Y/g, '(\\d+)') + .replace(/M/g, '(\\d+)') + .replace(/D/g, '(\\d+)') + .replace(/h/g, '(\\d+)') + .replace(/i/g, '(\\d+)') + .replace(/s/g, '(\\d+)'); const match = time.match(new RegExp(`^${tplRegex}$`)); @@ -50,21 +49,21 @@ export function formatDate(time = '', tpl = '') { throw new Error('Invalid time format'); } - //获取上海当前时间 + // Get the current Shanghai time const date = new Date(); - const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const hour = date.getHours() + 8; const minute = date.getMinutes(); - dateObj.Y = match.groups.Y || year; - dateObj.M = match.groups.M || month; - dateObj.D = match.groups.D || day; - dateObj.h = match.groups.h || hour; - dateObj.m = match.groups.i || minute; - dateObj.s = match.groups.s || 0; + // Map the matches to the corresponding date parts + dateObj.Y = match[1] || year; + dateObj.M = match[2] || month; + dateObj.D = match[3] || day; + dateObj.h = match[4] || hour; + dateObj.m = match[5] || minute; + dateObj.s = match[6] || 0; return new Date( dateObj.Y, @@ -75,3 +74,4 @@ export function formatDate(time = '', tpl = '') { dateObj.s ).getTime(); } +