Browse Source

fix:fixed parseTime bug in ie and safari

https://github.com/PanJiaChen/vue-element-admin/commit/776f10e19720039d39593064663b618fbdb0e837
潘嘉晨 5 years ago
parent
commit
97290e6f49
2 changed files with 13 additions and 2 deletions
  1. 10 2
      src/utils/index.js
  2. 3 0
      tests/unit/utils/parseTime.spec.js

+ 10 - 2
src/utils/index.js

@@ -17,9 +17,17 @@ export function parseTime(time, cFormat) {
17 17
   if (typeof time === 'object') {
18 18
     date = time
19 19
   } else {
20
-    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
21
-      time = parseInt(time)
20
+    if ((typeof time === 'string')) {
21
+      if ((/^[0-9]+$/.test(time))) {
22
+        // support "1548221490638"
23
+        time = parseInt(time)
24
+      } else {
25
+        // support safari
26
+        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
27
+        time = time.replace(new RegExp(/-/gm), '/')
28
+      }
22 29
     }
30
+
23 31
     if ((typeof time === 'number') && (time.toString().length === 10)) {
24 32
       time = time * 1000
25 33
     }

+ 3 - 0
tests/unit/utils/parseTime.spec.js

@@ -5,6 +5,9 @@ describe('Utils:parseTime', () => {
5 5
   it('timestamp', () => {
6 6
     expect(parseTime(d)).toBe('2018-07-13 17:54:01')
7 7
   })
8
+  it('timestamp string', () => {
9
+    expect(parseTime((d + ''))).toBe('2018-07-13 17:54:01')
10
+  })
8 11
   it('ten digits timestamp', () => {
9 12
     expect(parseTime((d / 1000).toFixed(0))).toBe('2018-07-13 17:54:01')
10 13
   })