-
Notifications
You must be signed in to change notification settings - Fork 89
/
dateUtil.js
365 lines (318 loc) · 9.62 KB
/
dateUtil.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* 此js文件主要包含日期方面的工具函数
*/
/**
* 获取当前时间的n天后的时间戳
* @param {number} n 天数
* @returns {Number} 返回值为时间毫秒值
*/
export function toNextTimes(n){
let timestamp = +new Date() + n * 86400000;
return timestamp;
}
/***
* 本周第一天
* @return {*} WeekFirstDay 返回本周第一天的时间
*/
export function showWeekFirstDay(){
let Nowdate=new Date();
let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
return WeekFirstDay;
}
/***
* 本周最后一天
* @return {*} WeekLastDay 返回本周最后一天的时间
*/
export function showWeekLastDay(){
let Nowdate=new Date();
let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
let WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
return WeekLastDay;
}
/***
* 本月第一天
* @return {*} MonthFirstDay 返回本月第一天的时间
*/
export function showMonthFirstDay(){
let Nowdate=new Date();
let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth());
return MonthFirstDay;
}
/***
* 本月最后一天
* @return {*} MonthLastDay 返回本月最后一天的时间
*/
export function showMonthLastDay(){
let Nowdate=new Date();
let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1);
let MonthLastDay=new Date(MonthNextFirstDay-86400000);
return MonthLastDay;
}
/**
* 日期转时间戳
* @param {String} time - 日期字符串,如'2018-8-8','2018,8,8','2018/8/8'
* @returns {Number} 返回值为时间毫秒值
*/
export function timeToTimestamp (time) {
let date = new Date(time);
let timestamp = date.getTime();
return timestamp;
}
/***
* 格式化当前时间
* @return {string} timeText 返回系统时间字符串
*/
export function getdataTimeSec() {
let time = new Date();
let weekDay;
let year = time.getFullYear();
let month = time.getMonth() + 1;
let day = time.getDate();
//获取时分秒
let h = time.getHours();
let m = time.getMinutes();
let s = time.getSeconds();
//检查是否小于10
h = check(h);
m = check(m);
s = check(s);
let now_day = time.getDay();
switch (now_day) {
case 0: {
weekDay = "星期日"
}
break;
case 1: {
weekDay = "星期一"
}
break;
case 2: {
weekDay = "星期二"
}
break;
case 3: {
weekDay = "星期三"
}
break;
case 4: {
weekDay = "星期四"
}
break;
case 5: {
weekDay = "星期五"
}
break;
case 6: {
weekDay = "星期六"
}
break;
case 7: {
weekDay = "星期日"
}
break;
}
let timeText = year + "年" + month + "月" + day + "日 " + " " + weekDay + " " + h + ":" + m +":" + s;
return timeText
}
/**
* 日期数字小于10,补“0”
*/
export function check(i) {
let num;
i < 10 ? num = "0" + i : num = i;
return num;
}
/**
* 返回指定时间戳之间的时间间隔
* @param {*} startTime 开始时间的时间戳
* @param {*} endTime 结束时间的时间戳
* @return {string} str 返回时间字符串
*/
export function getTimeInterval(startTime, endTime) {
let runTime = parseInt((endTime - startTime) / 1000);
let year = Math.floor(runTime / 86400 / 365);
runTime = runTime % (86400 * 365);
let month = Math.floor(runTime / 86400 / 30);
runTime = runTime % (86400 * 30);
let day = Math.floor(runTime / 86400);
runTime = runTime % 86400;
let hour = Math.floor(runTime / 3600);
runTime = runTime % 3600;
let minute = Math.floor(runTime / 60);
runTime = runTime % 60;
let second = runTime;
let str = '';
if (year > 0) {
str = year + '年';
}
if (year <= 0 && month > 0) {
str = month + '月';
}
if (year <= 0 && month <= 0 && day > 0) {
str = day + '天';
}
if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
str = hour + '小时';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
str = minute + '分钟';
}
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
str += second + '秒';
}
str += '前';
return str;
}
/**
* 按类型格式化日期
* @param {*} date 具体日期变量
* @param {string} dateType 需要返回类型
* @return {string} dateText 返回为指定格式的日期字符串
*/
export function getFormatDate(date, dateType) {
let dateObj = new Date(date);
let month = dateObj.getMonth() + 1;
let strDate = dateObj.getDate();
let hours = dateObj.getHours();
let minutes = dateObj.getMinutes();
let seconds = dateObj.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds
}
let dateText = dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate() + '日';
if (dateType == "yyyy-mm-dd") {
dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate();
}
if (dateType == "yyyy.mm.dd") {
dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate();
}
if (dateType == "yyyy-mm-dd MM:mm:ss") {
dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
}
if (dateType == "mm-dd MM:mm:ss") {
dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
}
if (dateType == "yyyy年mm月dd日 MM:mm:ss") {
dateText = dateObj.getFullYear() + '年' + month + '月' + strDate + '日' + ' ' + hours + ":" + minutes + ":" + seconds;
}
return dateText;
}
/**
* 判断是否为闰年
* @param {number} year 要判断的年份
* @return {boolean} 返回布尔值
*/
export function leapYear(year) {
return !(year % (year % 100 ? 4 : 400));
}
/**
* 返回两个年份之间的闰年
* @param {number} start 开始年份
* @param {number} end 结束年份
* @return {array} arr 返回符合闰年的数组
*/
export function leapYears(start, end) {
let arr = [];
for (var i=start; i<end; i++) {
if ( leapYear(i) ) {
arr.push(i)
}
}
return arr
}
/**
* 判断时间格式是否有效
* 短时间,如 (10:24:06)
* @param {string} str 需要验证的短时间
* @return {boolean} 返回布尔值
*/
export function isTime(str) {
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) { return false; }
if (a[1] >= 24 || a[3] >= 60 || a[4] >= 60) {
return false
}
return true;
}
/**
* 短日期,形如 (2019-10-24)
* @param {string} str 需要验证的短时间
* @return {boolean} 返回布尔值
*/
export function strDateTime(str){
var result = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4]);
return (d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]);
}
/**
* 长日期时间,形如 (2019-10-24 10:24:06)
* @param {string} str 需要验证的短时间
* @return {boolean} 返回布尔值
*/
export function strDateTime(str){
var result = str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4], result[5], result[6], result[7]);
return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4] && d.getHours() == result[5] && d.getMinutes() == result[6] && d.getSeconds() == result[7]);
}
/**
* 验证日期大小
* 例:"2019-10-24" 和 "2019-10-25"
* @param {string} d1需要验证的日期1
* @param {string} d2需要验证的日期2
* @return {boolean} 返回布尔值
*/
export function compareDate(d1, d2) {
return ((new Date(d1.replace(/-/g, "\/"))) < (new Date(d2.replace(/-/g, "\/"))));
}
/**
* 验证一个日期是不是今天
* @param {string} val 需要验证的日期
* @return {boolean} 返回布尔值
*/
export function isToday(val){
return new Date().toLocaleDateString() == new Date(val).toLocaleDateString();
}
/**
* 验证传入的日期是否是昨天
* @param {string} val 需要验证的日期
* @return {boolean} 返回布尔值
*/
export function isYesterday(val) {
var today = new Date();
var yesterday = new Date(now - 1000 * 60 * 60 * 24);
var test = new Date(val);
if (yesterday.getYear() === test.getYear() && yesterday.getMonth() === test.getMonth() && yesterday.getDate() === test.getDate()) {
return true;
} else {
return false;
}
}
/**
* 设置几天后的日期
* @param {string} date 起始日期
* @param {number} day 向后的天数
* @return {string} 返回想要得到的日期
*/
export function convertDate (date, day) {
let tempDate = new Date(date);
tempDate.setDate(tempDate.getDate()+day);
let Y = tempDate.getFullYear();
let M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1;
let D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate();
let result = Y + "-" + M + "-" + D
return result;
}