Vue 查看一周日期

本文介绍了一个使用Vue.js编写的动态周历组件,该组件根据当前日期生成一周的日历,并实现了点击切换选中样式的功能。通过`v-for`指令遍历一周的日期,结合`v-bind:class`实现选中状态的样式切换。同时,提供了`dateActive`方法用于获取选中的星期。
摘要由CSDN通过智能技术生成

效果
在这里插入图片描述

代码:
html代码:

<div class="week-item" v-on:click="dateActive(index)" v-for="(day,index) in week"  v-bind:class="{ active:index==current}">
     <div class="week-date">{{day.ts}}</div>
      <div class="week-name">{{day.xq}}</div>
 </div>

方法:

data: {
current 0//控制样式切换
week: [],//初始为空
}
                       created: function () {
 var currentTime = new Date();
     var nowDayOfWeek = currentTime.getDay();//当前周的第几天
     var nowDay = currentTime.getDate();//当前日
     var nowMonth = currentTime.getMonth();//当前月
     var nowYear = currentTime.getFullYear();//当前年
     currentTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);//完整日期
     var weeks = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
     for (var i = 0; i < 7; i++) {
         if (i == 0) {
            currentTime = currentTime.setDate(currentTime.getDate());
         } else {
            currentTime = currentTime.setDate(currentTime.getDate() + 1);
         }
         currentTime = new Date(currentTime);
         var currentDays = currentTime.getDate();
         var currentMon = currentTime.getMonth() + 1;
         var dateDay = currentDays//某一天
         var dates = {};
         dates.ts = currentMon + "-" + dateDay
         dates.xq = weeks[i]
         this.week.push(dates)
   }
                //this.weekstar = weekstar;
                //this.currentMon = currentMon;
            },

选中样式切换:

                dateActive: function (index) {
                    this.current = index;
                    var need = this.week[index].xq//获取选中的值
                    console.log(need)
                },
Vue中计算一周的时间可以使用以下代码: ```javascript const days = ref(\[''\]); const week = ref(); const weeks = \['星期一','星期二','星期三','星期四','星期五','星期六','星期日'\]; const generateDateModel = () => { const myDate = new Date(); const year = myDate.getFullYear(); const month = myDate.getMonth() + 1; week.value = myDate.getDay() - 1; let day = myDate.getDate(); week.value = weeks\[week.value\]; if(week.value === weeks\[0\]){ for(let j = 0; j < 7; j += 1){ days.value\[j\] = `${year}/${month}/${day + j}`; } }else{ const index = weeks.indexOf(week.value); day -= index; for(let j = 0; j < 7; j += 1){ days.value\[j\] = `${year}/${month}/${day + j}`; } } } generateDateModel(); ``` 这段代码会生成一个包含一周时间的数组`days`,其中每个元素都是一个日期字符串,格式为`年/月/日`。这个数组会根据当前日期自动计算,并且会根据当前是周一还是其他工作日来生成相应的日期。 #### 引用[.reference_title] - *1* [vue uniapp 计算一周日期、星期输出数组](https://blog.csdn.net/weixin_45395283/article/details/129982959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue获取当前一周日期](https://blog.csdn.net/weixin_51265675/article/details/127618890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue 获取一周时间](https://blog.csdn.net/weixin_44816309/article/details/123199285)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值