dateUtil.js
3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import moment from 'moment'
const DEFAULT_FORMAT = {
date: 'YYYY-MM-DD HH:mm:ss'
}
const DATE_FORMAT = {
date: 'YYYY-MM-DD'
}
const MONTH_FORMAT = {
date: 'YYYY-MM'
}
let dateUtil = {}
dateUtil = {
format (time, format) {
if (time !== null && time !== undefined) {
if (format === null || format === undefined) {
format = DEFAULT_FORMAT.date
}
return moment(time).format(format)
}
},
formatDate (time) {
if (time === null || time === undefined) {
time = new Date()
}
return moment(time).format(DATE_FORMAT.date)
},
formatMonth(time) {
if (time === null || time === undefined) {
time = new Date()
}
return moment(time).format(MONTH_FORMAT.date)
},
timestamp (obj, format) {
if (obj !== undefined && obj !== null) {
return moment(obj, format).valueOf()
}
},
date (obj, format) {
if (obj !== undefined && obj !== null) {
return moment(obj, format).toDate()
}
},
addMinutes (time, minutes, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(minutes, 'm').valueOf()
}
},
addHours (time, hours, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(hours, 'h').valueOf()
}
},
addDays (time, days, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(days, 'd').valueOf()
}
},
addWeeks (time, weeks, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(weeks, 'w').valueOf()
}
},
addMonths (time, months, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(months, 'M').valueOf()
}
},
addYears (time, years, format) {
if (time !== undefined && time !== null) {
return moment(time, format).add(years, 'y').valueOf()
}
},
today () {
return moment().startOf('day').valueOf()
},
now () {
return new Date().getTime()
},
/**
* 获取开始日期时间戳
* 如:获取昨天的开始时间戳
* this.$DateUtil.startOf('day', -1)
* 如:获取月的开始时间戳
* this.$DateUtil.startOf('Month')
* @param {*} type day;week;Month;Year
* @param {*} num 前后的差数
* @returns
*/
startOf (type, num) {
if (num === undefined) {
num = 0
}
return moment().add(num, type.substr(0, 1)).startOf(type).valueOf()
},
/**
* 获取结束日期时间戳
* 如:获取昨天的开始时间戳
* this.$DateUtil.endOf('day', -1)
* 如:获取月的结束时间戳
* this.$DateUtil.endOf('Month')
* @param {*} type day;week;Month;Year
* @param {*} num 前后的差数
* @returns
*/
endOf (type, num) {
if (num === undefined) {
num = 0
}
return moment().add(num, type.substr(0, 1)).endOf(type).valueOf()
}
}
export default dateUtil