vue获取当前的月份_vue根据选择的月份动态展示当前月份的每一天并展示每一天所对应的星期几...

时间: 2024-02-23 15:00:13 浏览: 19
可以通过Vue的computed属性来实现获取当前的月份,并通过v-for指令动态展示每一天,并通过JavaScript的Date对象获取每一天所对应的星期几。 以下是一个简单的实现示例: ```html <template> <div> <select v-model="selectedMonth"> <option v-for="(month, index) in months" :key="index" :value="month">{{ month }}</option> </select> <table> <thead> <tr> <th v-for="(day, index) in daysOfWeek" :key="index">{{ day }}</th> </tr> </thead> <tbody> <tr v-for="week in weeks" :key="week"> <td v-for="day in daysInMonth(week)" :key="day.date" :class="{ 'today': isToday(day.date) }"> {{ day.date }} <br /> {{ day.dayOfWeek }} </td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { selectedMonth: new Date().getMonth(), months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ], daysOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] }; }, computed: { currentYear() { return new Date().getFullYear(); }, currentMonth() { return this.selectedMonth; }, weeks() { const daysInMonth = this.daysInMonth(); const weekRows = []; let currentWeek = []; for (let i = 0; i < daysInMonth.length; i++) { currentWeek.push(daysInMonth[i]); if (daysInMonth[i].dayOfWeek === 'Sat' || i === daysInMonth.length - 1) { weekRows.push(currentWeek); currentWeek = []; } } return weekRows; } }, methods: { daysInMonth(week) { const daysInMonth = []; const year = this.currentYear; const month = this.currentMonth; const date = new Date(year, month, 1); while (date.getMonth() === month || daysInMonth.length < 35) { if (!week || date.getDay() === this.daysOfWeek.indexOf(week)) { daysInMonth.push({ date: date.getDate(), dayOfWeek: this.daysOfWeek[date.getDay()] }); } date.setDate(date.getDate() + 1); } return daysInMonth; }, isToday(date) { const today = new Date(); return this.currentYear === today.getFullYear() && this.currentMonth === today.getMonth() && date === today.getDate(); } } }; </script> <style> .today { background-color: #f0f0f0; } </style> ``` 在上面的示例中,我们首先定义了一个选项框,用户可以选择要展示的月份。然后,我们使用一个表格来展示当前月份的日期和星期几。我们使用computed属性来获取当前的年份、月份和星期几,并通过v-for指令动态展示每一天。我们还定义了一个方法来确定当前日期是否为今天,并将其标记为不同的颜色。

相关推荐

最新推荐

recommend-type

vue获取当前点击的元素并传值的实例

下面小编就为大家分享一篇vue获取当前点击的元素并传值的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

vue获取当前IP所在地天气.docx

原生的vue源代码,使用vue获取当前IP,并且获取到当前所在地天气.docx,亲测有效,有问题可一直私聊我
recommend-type

Vue 按照创建时间和当前时间显示操作(刚刚,几小时前,几天前)

主要介绍了Vue 按照创建时间和当前时间显示操作(刚刚,几小时前,几天前),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Vue如何获取数据列表展示

主要为大家详细介绍了Vue如何获取数据列表展示,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

解决vue项目中某一页面不想引用公共组件app.vue的问题

主要介绍了解决vue项目中某一页面不想引用公共组件app.vue的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。