From 4597711d9d08f16cae1a388fa6305bdb9892d906 Mon Sep 17 00:00:00 2001 From: Aisen60 <1147319086@qq.com> Date: Fri, 20 Mar 2020 21:24:31 +0800 Subject: [PATCH] fix:fixed parseTime bug in ie and safari(#3066) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * /src/utils/index.js parseTime 添加IE浏览器(版本10以下,包括版本10)兼容。 * perf: update Co-authored-by: aisen60 Co-authored-by: 花裤衩 --- src/utils/index.js | 12 ++++++++++-- tests/unit/utils/parseTime.spec.js | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 2684e3c2..eb760d5e 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -17,9 +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) + 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 77ecb9d5..bc61d1ac 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') })