提交 | 用户 | age
|
2a61f6
|
1 |
/** |
L |
2 |
* Created by PanJiaChen on 16/11/18. |
|
3 |
*/ |
|
4 |
|
|
5 |
/** |
|
6 |
* Parse the time to string |
|
7 |
* @param {(Object|string|number)} time |
|
8 |
* @param {string} cFormat |
|
9 |
* @returns {string | null} |
|
10 |
*/ |
|
11 |
export function parseTime(time, cFormat) { |
|
12 |
if (arguments.length === 0 || !time) { |
|
13 |
return null |
|
14 |
} |
|
15 |
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' |
|
16 |
let date |
|
17 |
if (typeof time === 'object') { |
|
18 |
date = time |
|
19 |
} else { |
|
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 |
} |
|
29 |
} |
|
30 |
|
|
31 |
if ((typeof time === 'number') && (time.toString().length === 10)) { |
|
32 |
time = time * 1000 |
|
33 |
} |
|
34 |
date = new Date(time) |
|
35 |
} |
|
36 |
const formatObj = { |
|
37 |
y: date.getFullYear(), |
|
38 |
m: date.getMonth() + 1, |
|
39 |
d: date.getDate(), |
|
40 |
h: date.getHours(), |
|
41 |
i: date.getMinutes(), |
|
42 |
s: date.getSeconds(), |
|
43 |
a: date.getDay() |
|
44 |
} |
|
45 |
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { |
|
46 |
const value = formatObj[key] |
|
47 |
// Note: getDay() returns 0 on Sunday |
|
48 |
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] } |
|
49 |
return value.toString().padStart(2, '0') |
|
50 |
}) |
|
51 |
return time_str |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* @param {number} time |
|
56 |
* @param {string} option |
|
57 |
* @returns {string} |
|
58 |
*/ |
|
59 |
export function formatTime(time, option) { |
|
60 |
if (('' + time).length === 10) { |
|
61 |
time = parseInt(time) * 1000 |
|
62 |
} else { |
|
63 |
time = +time |
|
64 |
} |
|
65 |
const d = new Date(time) |
|
66 |
const now = Date.now() |
|
67 |
|
|
68 |
const diff = (now - d) / 1000 |
|
69 |
|
|
70 |
if (diff < 30) { |
|
71 |
return '刚刚' |
|
72 |
} else if (diff < 3600) { |
|
73 |
// less 1 hour |
|
74 |
return Math.ceil(diff / 60) + '分钟前' |
|
75 |
} else if (diff < 3600 * 24) { |
|
76 |
return Math.ceil(diff / 3600) + '小时前' |
|
77 |
} else if (diff < 3600 * 24 * 2) { |
|
78 |
return '1天前' |
|
79 |
} |
|
80 |
if (option) { |
|
81 |
return parseTime(time, option) |
|
82 |
} else { |
|
83 |
return ( |
|
84 |
d.getMonth() + |
|
85 |
1 + |
|
86 |
'月' + |
|
87 |
d.getDate() + |
|
88 |
'日' + |
|
89 |
d.getHours() + |
|
90 |
'时' + |
|
91 |
d.getMinutes() + |
|
92 |
'分' |
|
93 |
) |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* @param {string} url |
|
99 |
* @returns {Object} |
|
100 |
*/ |
|
101 |
export function param2Obj(url) { |
|
102 |
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') |
|
103 |
if (!search) { |
|
104 |
return {} |
|
105 |
} |
|
106 |
const obj = {} |
|
107 |
const searchArr = search.split('&') |
|
108 |
searchArr.forEach(v => { |
|
109 |
const index = v.indexOf('=') |
|
110 |
if (index !== -1) { |
|
111 |
const name = v.substring(0, index) |
|
112 |
const val = v.substring(index + 1, v.length) |
|
113 |
obj[name] = val |
|
114 |
} |
|
115 |
}) |
|
116 |
return obj |
|
117 |
} |