手写vue日历组件

vue版本vue2,样式和参数参考element-ui的Calendar组件。自动补全上一月和下一月空白日期,支持周起始日设置,单元格日期自定义插槽,自定义右侧头部插槽。
坑点:闰年2月29天处理

处理闰年问题代码

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
//判断是否为闰年
funtion isLeapYear(year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

//获取当月的天数
funtion getDaysOfMonth(dateStr) {
var date = new Date(dateStr);
var year = date.getFullYear();
var mouth = date.getMonth() + 1;
var day = 0;

if (mouth == 2) {
day = isLeapYear(year) ? 29 : 28;
} else if (
mouth == 1 ||
mouth == 3 ||
mouth == 5 ||
mouth == 7 ||
mouth == 8 ||
mouth == 10 ||
mouth == 12
) {
day = 31;
} else {
day = 30;
}
return day;
},

完整代码

分为头部和日历天数区域,dom结构和样式代码
参数
v-model 绑定值 Date, String, Number
first-day-of-week 周起始日 Number 默认值 1

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
<template>
<div class="calendar">
<div class="calendar-header">
<div class="left">
<span class="month">{{ value.toString("yyyy年MM月") }}</span>
<button @click="btnChangeMonth('prevMonth')">上个月</button>
<button style="margin-left: 10px" @click="btnChangeMonth('nextMonth')">
下个月
</button>
</div>
<div class="right">
<!-- 自定义右侧头部插槽 -->
<slot name="right-header"></slot>
</div>
</div>

<div class="calendar-body">
<table class="table">
<thead>
<tr>
<td>
{{ week[firstDayOfWeek] }}
</td>
<td
v-for="item in week.length - 1 - firstDayOfWeek"
:key="week[item + firstDayOfWeek]"
>

{{ week[item + firstDayOfWeek] }}
</td>
<td v-for="item in firstDayOfWeek" :key="week[item - 1]">
{{ week[item - 1] }}
</td>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in days.length / 7" :key="index">
<td
:class="{ empty: !days[day + index * 7 - 1].date }"
v-for="day in 7"
:key="day + index * 7"
>

<!-- 单元格日期自定义插槽 -->
<slot name="dateCell" :row="days[day + index * 7 - 1]">
{{
days[day + index * 7 - 1] &&
days[day + index * 7 - 1].date.split("-")[2]
}}
</slot>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>

<script>
export default {
name: "Calendar",
model: {
prop: "value",
event: "monthChange",
},
props: {
value: [Date, String, Number],
firstDayOfWeek: {
type: Number,
default: 1,
},
},
data() {
return {
week: ["日", "一", "二", "三", "四", "五", "六"],
days: [],
};
},
methods: {
btnChangeMonth(month) {
// 上一月
if (month === "prevMonth") {
this.$emit("monthChange", this.getPrevNextMonth(this.value, -1));
}
// 下一月
if (month === "nextMonth") {
this.$emit("monthChange", this.getPrevNextMonth(this.value, 1));
}
},
init() {
const date = this.value;
const time = date.toString("yyyy-MM");
const firstDayOfWeek = this.firstDayOfWeek;
// 当月周一是星期几 0-6 0是星期日
const days = this.getMonthDay(date);
const fillDay = [];
let day = [];
// 一个月多少天
const monthDay = this.getDaysOfMonth(date);
// 补全空格上月日期
const fill = (7 + days - firstDayOfWeek) % 7;
for (let i = 0; i < fill; i++) {
fillDay.push({ date: "" });
}

for (let j = 1; j <= monthDay; j++) {
let d = j.toString().padStart(2, "0");
day.push({ date: time + "-" + d });
}
day = [...fillDay, ...day];
// 补全空格下个月日期
while (day.length % 7 != 0) {
day.push({ date: "" });
}
this.$set(this, "days", day);
},
//获取当月的天数
getDaysOfMonth(dateStr) {
var date = new Date(dateStr);
var year = date.getFullYear();
var mouth = date.getMonth() + 1;
var day = 0;

if (mouth == 2) {
day = this.isLeapYear(year) ? 29 : 28;
} else if (
mouth == 1 ||
mouth == 3 ||
mouth == 5 ||
mouth == 7 ||
mouth == 8 ||
mouth == 10 ||
mouth == 12
) {
day = 31;
} else {
day = 30;
}
return day;
},
//判断是否为闰年
isLeapYear(year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
},

/**
* 获取上一月或者下一个月
* @param now 日期
* @param addMonths 传-1 上个月,传1 下个月
*/

getPrevNextMonth(now, addMonths) {
var dd = new Date(now);
var m = dd.getMonth() + 1;
var y =
dd.getMonth() + 1 + addMonths > 12
? dd.getFullYear() + 1
: dd.getFullYear();
if (m + addMonths == 0) {
y = y - 1;
m = 12;
} else {
if (m + addMonths > 12) {
m = "01";
} else {
m = m + 1 < 10 ? "0" + (m + addMonths) : m + addMonths;
}
}
return new Date(y, m, 0);
},

// 获取月份1号是周几
getMonthDay(date) {
var d = new Date(date);
const y = d.getFullYear();
const m = d.getMonth();
return new Date(y, m, 1).getDay();
},
},
created() {
this.init();
},
watch: {
value(val) {
this.init();
},
},
};
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.calendar {
width: 100%;
background: #fff;
.calendar-header {
overflow: hidden;
.left {
float: left;
.month {
display: inline-block;
width: 96px;
font-size: 16px;
line-height: 32px;
font-weight: 500;
color: #333;
}
}
.right {
float: right;
}
}
.calendar-body {
width: 100%;
padding: 0 1px;
overflow-y: auto;
.table {
width: 100%;
border-collapse: collapse;
box-sizing: border-box;
thead {
td {
padding: 8px 0;
text-align: center;
}
}
tbody {
td {
width: 14.28%;
height: 80px;
border: 1px solid #ebeef5;
font-size: 18px;
padding: 8px 14px;
color: #666;
&.empty {
background: #f9f9f9;
}
}
}
}
}
}
</style>

用法

所有插槽和参数的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 自定义左侧头部
<Calendar v-model="new Date()">
<div slot="right-header" style="color: red">自定义左侧头部</div>
</Calendar>

// 自定义日期
<Calendar v-model="new Date()">
<template slot="dateCell" slot-scope="scope">
{{ scope.row.date.split("-").slice(1).join("-") }}
</template>
</Calendar>

// 自定义周起始日
<Calendar v-model="new Date()" :first-day-of-week="0"> </Calendar>

demo

demo地址