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
export function getDateMap(minYear, maxYear) {
const DateMap = new Map();
const getOneYear = (year) => {
const yearDays = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 366 : 365;
for (let i = 1; i <= yearDays; i++) {
DateMap.set(toLocaleDateString(new Date(year, 0, i)), []);
}
}
for (let i = minYear; i <= maxYear; i++) {
getOneYear(i);
}
return DateMap
}
// 返回删除的日期
/**
* 如果删除单次重复会议返回['2020-04-20'],
* 如果删除此次及以后重复会议返回['future2020-04-20']
*/
export function getExcludeDate(data = []) {
const excludeDateList = [];
const futureList = [];
data.forEach(item => {
//删除此次及以后
if (item.length > 10) {
futureList.push(new Date(item.slice(6, 16).replace(/-/g, "/")).getTime());
}
//删除单次
else {
excludeDateList.push(item);
}
});
return {
excludeDateList,
minFutureTime: Math.min(...futureList)
};
}
// 返回跨天数
export function getNextDateList(startTime, endTime) {
let days = 0;
const nextDateList = [];
if (startTime.getDate() !== endTime.getDate()) {
//同一个月
if (startTime.getMonth() == endTime.getMonth()) {
days = endTime.getDate() - startTime.getDate();
}
//跨月
else {
// 一个月份有多少天new Date(year, month, 0).getDate()
const startMonth = new Date(startTime.getFullYear(), startTime.getMonth(), 0).getDate();
days = startMonth - startTime.getDate() + endTime.getDate()
}
} else {
//跨一个月 不考虑
}
//返回跨天的日期数组
for (let i = 0; i <= days; i++) {
const newDate = new Date(startTime);
const nextDate = toLocaleDateString(new Date(newDate.setDate(newDate.getDate() + i)));
nextDateList.push(nextDate)
}
return nextDateList
}
export function getWeekNumber(year, month, days) {
const isLeapYear = (year) => (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)
const getMonthDays = (year, month) => [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
//那一天是那一年中的第多少天
for (var i = 0; i < month; i++) {
days += getMonthDays(year, i);
}
//那一年第一天是星期几
var yearFirstDay = new Date(year, 0, 1).getDay();
var week = null;
if (yearFirstDay == 0) {
week = Math.ceil(days / 7);
} else {
days -= (6 - yearFirstDay + 1);
week = Math.ceil(days / 7);
}
return week;
}
export function getBlankList(year) {
const dateMap = getDateMap(year, year);
const DateList = [];
dateMap.forEach(function (value, key) {
const keyDate = new Date(key);
const year = keyDate.getFullYear();
const month = keyDate.getMonth();
const date = keyDate.getDate();
const day = keyDate.getDay();
// 生成年
if (month === 0 && date === 1) {
DateList.push({
type: "year",
value: year,
dateStr: `${year}`
});
}
//生成月
if (date === 1) {
DateList.push({
type: "month",
value: month + 1,
dateStr: `${year}/${month + 1}`
});
}
//生成周
if (day === 0) {
const rangeDate = new Date(year, month, date + 6);
const rangeMonth2 = new Date(rangeDate).getMonth();
const rangeDay2 = new Date(rangeDate).getDate();
DateList.push({
type: "week",
value: `第${getWeekNumber(year, month, date)}周,${month + 1}月${date}日 - ${
rangeMonth2 == month ? "" : rangeMonth2 + 1 + "月"
}${rangeDay2}日`,
dateStr: `${year}/${month + 1}/${date}-week`
});
}
})
return DateList;
}
export function toLocaleDateString(date) {
if (typeof date === 'string') {
date = new Date(date)
}
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
}