|
@@ -2,57 +2,57 @@
|
2
|
2
|
* Created by jiachenpan on 16/11/18.
|
3
|
3
|
*/
|
4
|
4
|
|
5
|
|
- export function parseTime(time, cFormat) {
|
6
|
|
- if (arguments.length === 0) {
|
7
|
|
- return null
|
8
|
|
- }
|
9
|
|
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
10
|
|
- let date
|
11
|
|
- if (typeof time === 'object') {
|
12
|
|
- date = time
|
13
|
|
- } else {
|
14
|
|
- if (('' + time).length === 10) time = parseInt(time) * 1000
|
15
|
|
- date = new Date(time)
|
16
|
|
- }
|
17
|
|
- const formatObj = {
|
18
|
|
- y: date.getFullYear(),
|
19
|
|
- m: date.getMonth() + 1,
|
20
|
|
- d: date.getDate(),
|
21
|
|
- h: date.getHours(),
|
22
|
|
- i: date.getMinutes(),
|
23
|
|
- s: date.getSeconds(),
|
24
|
|
- a: date.getDay()
|
25
|
|
- }
|
26
|
|
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
27
|
|
- let value = formatObj[key]
|
28
|
|
- if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
|
29
|
|
- if (result.length > 0 && value < 10) {
|
30
|
|
- value = '0' + value
|
31
|
|
- }
|
32
|
|
- return value || 0
|
33
|
|
- })
|
34
|
|
- return time_str
|
35
|
|
- }
|
|
5
|
+export function parseTime(time, cFormat) {
|
|
6
|
+ if (arguments.length === 0) {
|
|
7
|
+ return null
|
|
8
|
+ }
|
|
9
|
+ const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
10
|
+ let date
|
|
11
|
+ if (typeof time === 'object') {
|
|
12
|
+ date = time
|
|
13
|
+ } else {
|
|
14
|
+ if (('' + time).length === 10) time = parseInt(time) * 1000
|
|
15
|
+ date = new Date(time)
|
|
16
|
+ }
|
|
17
|
+ const formatObj = {
|
|
18
|
+ y: date.getFullYear(),
|
|
19
|
+ m: date.getMonth() + 1,
|
|
20
|
+ d: date.getDate(),
|
|
21
|
+ h: date.getHours(),
|
|
22
|
+ i: date.getMinutes(),
|
|
23
|
+ s: date.getSeconds(),
|
|
24
|
+ a: date.getDay()
|
|
25
|
+ }
|
|
26
|
+ const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
27
|
+ let value = formatObj[key]
|
|
28
|
+ if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
|
|
29
|
+ if (result.length > 0 && value < 10) {
|
|
30
|
+ value = '0' + value
|
|
31
|
+ }
|
|
32
|
+ return value || 0
|
|
33
|
+ })
|
|
34
|
+ return time_str
|
|
35
|
+}
|
36
|
36
|
|
37
|
|
- export function formatTime(time, option) {
|
38
|
|
- time = +time * 1000
|
39
|
|
- const d = new Date(time)
|
40
|
|
- const now = Date.now()
|
|
37
|
+export function formatTime(time, option) {
|
|
38
|
+ time = +time * 1000
|
|
39
|
+ const d = new Date(time)
|
|
40
|
+ const now = Date.now()
|
41
|
41
|
|
42
|
|
- const diff = (now - d) / 1000
|
|
42
|
+ const diff = (now - d) / 1000
|
43
|
43
|
|
44
|
|
- if (diff < 30) {
|
45
|
|
- return '刚刚'
|
46
|
|
- } else if (diff < 3600) { // less 1 hour
|
47
|
|
- return Math.ceil(diff / 60) + '分钟前'
|
48
|
|
- } else if (diff < 3600 * 24) {
|
49
|
|
- return Math.ceil(diff / 3600) + '小时前'
|
50
|
|
- } else if (diff < 3600 * 24 * 2) {
|
51
|
|
- return '1天前'
|
52
|
|
- }
|
53
|
|
- if (option) {
|
54
|
|
- return parseTime(time, option)
|
55
|
|
- } else {
|
56
|
|
- return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
|
57
|
|
- }
|
58
|
|
- }
|
|
44
|
+ if (diff < 30) {
|
|
45
|
+ return '刚刚'
|
|
46
|
+ } else if (diff < 3600) { // less 1 hour
|
|
47
|
+ return Math.ceil(diff / 60) + '分钟前'
|
|
48
|
+ } else if (diff < 3600 * 24) {
|
|
49
|
+ return Math.ceil(diff / 3600) + '小时前'
|
|
50
|
+ } else if (diff < 3600 * 24 * 2) {
|
|
51
|
+ return '1天前'
|
|
52
|
+ }
|
|
53
|
+ if (option) {
|
|
54
|
+ return parseTime(time, option)
|
|
55
|
+ } else {
|
|
56
|
+ return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
|
|
57
|
+ }
|
|
58
|
+}
|