<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="./index.css" />
<script src="./vue.js"></script>
<script src="./index.js"></script>
<script src="./dayjs.min.js"></script>
</head>
<body>
<div id="myApp">
<div class="block">
<span class="demonstration">月</span>
<el-date-picker
@change="changeValue"
v-model="value2"
type="month"
placeholder="选择月"
>
</el-date-picker>
</div>
<table>
<tr v-for="weekNum in weeks">
<td v-for="dayNum in 7">{{ getNum(weekNum,dayNum) }}</td>
</tr>
</table>
</div>
<script>
new Vue({
el: "#myApp",
data() {
return {
value2: "",
weeks: 0,
count1: 0,
count2: 0,
day2: 0,
day1: 0,
currentDay: 0,
};
},
methods: {
changeValue(res) {
let fullYear = res.getFullYear();
let month = res.getMonth();
let daysInMonth = dayjs(res).daysInMonth();
this.day1 =
new Date(fullYear, month).getDay() === 0
? 7
: new Date(fullYear, month).getDay();
this.day2 =
new Date(fullYear, month, daysInMonth).getDay() === 0
? 7
: new Date(fullYear, month, daysInMonth).getDay();
this.count1 = this.day1 - 1;
this.count2 = 7 - this.day2;
let totalDays = daysInMonth + this.count1 + this.count2;
this.weeks = totalDays / 7;
console.log(
fullYear,
month,
daysInMonth,
this.day1,
this.day2,
this.count1,
this.count2,
totalDays,
this.weeks
);
},
getNum(weekNum, dayNum) {
if (weekNum === 1) {
if (dayNum <= this.count1) {
return "";
} else {
return dayNum - this.count1;
}
} else if (weekNum === this.weeks) {
if (dayNum > this.day2) {
return "";
} else {
return 7 - this.count1 + dayNum + (weekNum - 2) * 7;
}
} else {
return 7 - this.count1 + dayNum + (weekNum - 2) * 7;
}
},
},
});
</script>
</body>
</html>