-
Notifications
You must be signed in to change notification settings - Fork 1
/
dayBorder.html
78 lines (70 loc) · 2.12 KB
/
dayBorder.html
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
<script>
/**
* 根据 年份 和 季度 获取 季度 第一天 和 季度 最后 一天
* @param year
* @param quarter
*/
const getQuartorStartDate = (d) => {
const month = d.getMonth();
const year = d.getFullYear();
const quarterRange = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]];
let startMonth = 1;
let endMonth = 1;
quarterRange.forEach(element => {
if (element.includes(month)) {
startMonth = element.shift();
endMonth = startMonth + 2;
};
});
const startDate = d.getFullYear() + "-" + formatDate(startMonth) + d.getDate();
const endDate = d.getFullYear() + '-' + formatDate(endMonth) + '-' + new Date(year, endMonth, 0).getDate();
return {
startDate, endDate
};
};
// 格式化月和日为MM、dd
const formatDate = (value) => {
if (value < 10) {
value = "0" + value;
}
return value;
};
const getMonthStartDate = (d) => {
let startMon;
let month = d.getMonth();
startMon = formatDate(month);
let lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();// 获取当月最后一天日期
let startDate = d.getFullYear() + '-' + startMon + '-' + "0" + d.getDate();
let endDate = d.getFullYear() + '-' + startMon + '-' + lastDay;
return {
startDate, endDate
};
};
const getYearStartDate = (d) => {
let startMon = '01';
let endMon = "12";
var lastDay = new Date(d.getTime() - 1000 * 60 * 60 * 24).getDate();// 获取当月最后一天日期
let startDate = d.getFullYear() + '-' + startMon + '-' + "0" + d.getDate();
let endDate = d.getFullYear() + '-' + endMon + '-' + lastDay;
return {
startDate, endDate
};
};
const getTimeBorder = (timeStamp = '', type) => {
let date = timeStamp ? new Date(timeStamp) : new Date();
let month = date.getMonth() + 1;
var d = new Date(date.getFullYear(), month, 1); // 取当年当月中的第一天
if (type === "year") {
return getYearStartDate(d);
}
if (type === 'month') {
return getMonthStartDate(d);
}
if (type === 'quartor') {
return getQuartorStartDate(d);
}
};
let border = getTimeBorder('', 'month');
console.log(border.startDate);
console.log(border.endDate);
</script>