|
@@ -1,79 +0,0 @@
|
1
|
|
-/**
|
2
|
|
- * Created by jiachenpan on 16/11/18.
|
3
|
|
- */
|
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 ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
15
|
|
- time = parseInt(time)
|
16
|
|
- }
|
17
|
|
- if ((typeof time === 'number') && (time.toString().length === 10)) {
|
18
|
|
- time = time * 1000
|
19
|
|
- }
|
20
|
|
- date = new Date(time)
|
21
|
|
- }
|
22
|
|
- const formatObj = {
|
23
|
|
- y: date.getFullYear(),
|
24
|
|
- m: date.getMonth() + 1,
|
25
|
|
- d: date.getDate(),
|
26
|
|
- h: date.getHours(),
|
27
|
|
- i: date.getMinutes(),
|
28
|
|
- s: date.getSeconds(),
|
29
|
|
- a: date.getDay()
|
30
|
|
- }
|
31
|
|
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
32
|
|
- let value = formatObj[key]
|
33
|
|
- // Note: getDay() returns 0 on Sunday
|
34
|
|
- if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
|
35
|
|
- if (result.length > 0 && value < 10) {
|
36
|
|
- value = '0' + value
|
37
|
|
- }
|
38
|
|
- return value || 0
|
39
|
|
- })
|
40
|
|
- return time_str
|
41
|
|
-}
|
42
|
|
-
|
43
|
|
-export function formatTime(time, option) {
|
44
|
|
- time = +time * 1000
|
45
|
|
- const d = new Date(time)
|
46
|
|
- const now = Date.now()
|
47
|
|
-
|
48
|
|
- const diff = (now - d) / 1000
|
49
|
|
-
|
50
|
|
- if (diff < 30) {
|
51
|
|
- return '刚刚'
|
52
|
|
- } else if (diff < 3600) {
|
53
|
|
- // less 1 hour
|
54
|
|
- return Math.ceil(diff / 60) + '分钟前'
|
55
|
|
- } else if (diff < 3600 * 24) {
|
56
|
|
- return Math.ceil(diff / 3600) + '小时前'
|
57
|
|
- } else if (diff < 3600 * 24 * 2) {
|
58
|
|
- return '1天前'
|
59
|
|
- }
|
60
|
|
- if (option) {
|
61
|
|
- return parseTime(time, option)
|
62
|
|
- } else {
|
63
|
|
- return (
|
64
|
|
- d.getMonth() +
|
65
|
|
- 1 +
|
66
|
|
- '月' +
|
67
|
|
- d.getDate() +
|
68
|
|
- '日' +
|
69
|
|
- d.getHours() +
|
70
|
|
- '时' +
|
71
|
|
- d.getMinutes() +
|
72
|
|
- '分'
|
73
|
|
- )
|
74
|
|
- }
|
75
|
|
-}
|
76
|
|
-
|
77
|
|
-export function isExternal(path) {
|
78
|
|
- return /^(https?:|mailto:|tel:)/.test(path)
|
79
|
|
-}
|