From d54cd25de27ce1154661a1d1edd38cf9c4b6872e Mon Sep 17 00:00:00 2001 From: aisen60 Date: Fri, 20 Mar 2020 00:08:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?/src/utils/index.js=20parseTime=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0IE=E6=B5=8F=E8=A7=88=E5=99=A8=EF=BC=88=E7=89=88?= =?UTF-8?q?=E6=9C=AC10=E4=BB=A5=E4=B8=8B=EF=BC=8C=E5=8C=85=E6=8B=AC?= =?UTF-8?q?=E7=89=88=E6=9C=AC10=EF=BC=89=E5=85=BC=E5=AE=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils/index.js b/src/utils/index.js index 2684e3c2597..b50e3bd0f82 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -19,6 +19,8 @@ export function parseTime(time, cFormat) { } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) + } else if (typeof time === 'string') { + time = time.replace(new RegExp(/-/gm), '/'); } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 From 1877997fb640e36437b1b198fcf18ad80bfc5ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E8=A3=A4=E8=A1=A9?= Date: Fri, 20 Mar 2020 19:56:46 +0800 Subject: [PATCH 2/2] perf: update --- src/utils/index.js | 14 ++++++++++---- tests/unit/utils/parseTime.spec.js | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index b50e3bd0f82..eb760d5e06e 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -17,11 +17,17 @@ export function parseTime(time, cFormat) { if (typeof time === 'object') { 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), '/'); + if ((typeof time === 'string')) { + if ((/^[0-9]+$/.test(time))) { + // support "1548221490638" + time = parseInt(time) + } else { + // support safari + // https://stackoverflow.com/questions/4310953/invalid-date-in-safari + time = time.replace(new RegExp(/-/gm), '/') + } } + if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } diff --git a/tests/unit/utils/parseTime.spec.js b/tests/unit/utils/parseTime.spec.js index 77ecb9d53c2..bc61d1ac1d8 100644 --- a/tests/unit/utils/parseTime.spec.js +++ b/tests/unit/utils/parseTime.spec.js @@ -4,6 +4,11 @@ describe('Utils:parseTime', () => { it('timestamp', () => { expect(parseTime(d)).toBe('2018-07-13 17:54:01') }) + + it('timestamp string', () => { + expect(parseTime((d + ''))).toBe('2018-07-13 17:54:01') + }) + it('ten digits timestamp', () => { expect(parseTime((d / 1000).toFixed(0))).toBe('2018-07-13 17:54:01') })