vue实现日期选择器,只显示周一到周五的日期

时间:2025-04-08 15:05:56
  • data() {
  • return {
  • timeValue: "",
  • teheadList: ["日", "一", "二", "三", "四", "五", "六"],
  • year: "",
  • month: 0,
  • day: "",
  • tbodyList: [],
  • currentDay: "",
  • show:false
  • };
  • },
  • mounted() {
  • this.getInitTime();
  • },
  • watch: {
  • month() {
  • if (this.month < 1) {
  • this.month = 12;
  • this.year--;
  • } else if (this.month > 12) {
  • this.month = 1;
  • this.year++;
  • }
  • this.tbodyList = [];
  • this.timeValue = this.year + "年" + this.month + "月";
  • this.handleAetCalendar();
  • },
  • },
  • methods: {
  • getInitTime() {
  • let date = new Date();
  • this.year = date.getFullYear(); // 获取当年
  • this.month = date.getMonth() + 1; // 获取本月
  • this.day = date.getDate(); // 获取当天
  • this.timeValue = date.getFullYear() + "年" + (date.getMonth() + 1) + "月";
  • this.handleAetCalendar();
  • },
  • // 生成档期数据
  • handleAetCalendar() {
  • this.tbodyList = [];
  • let days = new Date(this.year, this.month, 0).getDate(); // 当月总天数
  • let week = new Date(this.year, this.month - 1, 1).getDay(); // 当月有几周
  • let last_month = new Date(this.year, this.month + 1, 0).getDate(); // 当月的上一个月的最后一天
  • console.log("nn", this.tbodyList);
  • this.tbodyList[0] = [];
  • for (let i = 0; i < Math.ceil((days + week) / 7) * 7; i++) {
  • let nub = Math.floor(i / 7);
  • if (i < week) {
  • // ('nub',nub,[nub]);
  • this.tbodyList[nub].push({
  • class: "default",
  • name: last_month + i - week + 1,
  • month: this.month == 0 ? 12 : this.month - 1,
  • });
  • } else {
  • if (!this.tbodyList[nub]) {
  • this.tbodyList[nub] = [];
  • }
  • let day = i - week + 1;
  • let calssName = "actives";
  • let month = this.month;
  • if (day > days) {
  • day -= days;
  • calssName = "default";
  • month = this.month + 1 > 12 ? 1 : this.month + 1;
  • }
  • this.tbodyList[nub].push({
  • class: calssName,
  • name: day,
  • month: month,
  • });
  • }
  • }
  • let arr = this.tbodyList[Math.floor((week + days) / 7)];
  • if (arr && arr.length !== 7) {
  • this.tbodyList[Math.floor((week + days) / 7)] = arr.concat(
  • new Array(7 - arr.length).fill("")
  • );
  • }
  • },
  • },