From 97290e6f49eba1b4c1ede14a2d8c70fb5f363386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=98=E5=98=89=E6=99=A8?= Date: Thu, 4 Jun 2020 21:19:22 +0800 Subject: [PATCH] fix:fixed parseTime bug in ie and safari https://github.com/PanJiaChen/vue-element-admin/commit/776f10e19720039d39593064663b618fbdb0e837 --- src/utils/index.js | 12 ++++++++++-- tests/unit/utils/parseTime.spec.js | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 7e3fc1a..61e7704 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 41d1b02..d1ac0d8 100644 --- a/tests/unit/utils/parseTime.spec.js +++ b/tests/unit/utils/parseTime.spec.js @@ -5,6 +5,9 @@ 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') })