欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 解决了使用ElementUI日期选择器时,选择“2024第1周”这一特殊时间段所触发的change事件返回时间的特殊情况处理问题。

解决了使用ElementUI日期选择器时,选择“2024第1周”这一特殊时间段所触发的change事件返回时间的特殊情况处理问题。

2025/3/26 4:08:56 来源:https://blog.csdn.net/qq_45149139/article/details/146425587  浏览:    关键词:解决了使用ElementUI日期选择器时,选择“2024第1周”这一特殊时间段所触发的change事件返回时间的特殊情况处理问题。
  • 当选择某周后,自动计算该周的特殊日期范围:
    • 将ElementUI返回的周一日期减1天,得到上周日作为起始日
    • 从起始日加6天,得到本周六作为结束日
  • 选示例:选择"2024 第 1 周"时
    • 原始周一日期:2023-12-31(ElementUI默认返回周日)
    • 计算后范围:2024-01-06(周日)至 2025-03-22(周六)

代码:

<template><div><el-date-pickerv-model="value1"type="week"format="yyyy 第 WW 周"placeholder="选择周"@change="handleDateChange"/><div v-if="selectedYear && selectedWeekStart && selectedWeekEnd"><p>选中的年份: {{ selectedYear }}</p><p>选中周的第一天(减一): {{ selectedWeekStart }}</p><p>选中周的最后一天(减一): {{ selectedWeekEnd }}</p></div></div>
</template><script>
export default {data() {return {value1: '', // 绑定的值selectedYear: null, // 选中的年份selectedWeekStart: null, // 选中周的第一天(减一)selectedWeekEnd: null // 选中周的最后一天(减一)}},methods: {handleDateChange(value) {if (value) {const selectedDate = new Date(value)// 计算周一的日期(ElementUI默认返回周一的日期)const startDate = new Date(selectedDate)// 对周一的日期减一,得到上周日startDate.setDate(startDate.getDate() - 1)// 计算周日的日期const endDate = new Date(startDate)endDate.setDate(startDate.getDate() + 6) // 从上周日开始加6天,得到本周六this.selectedYear = startDate.getFullYear()this.selectedWeekStart = this.formatDate(startDate)this.selectedWeekEnd = this.formatDate(endDate)} else {// 重置选中的日期信息this.selectedYear = nullthis.selectedWeekStart = nullthis.selectedWeekEnd = null}},formatDate(date) {const year = date.getFullYear()const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1const day = String(date.getDate()).padStart(2, '0') // 直接使用 date.getDate(),不需要减1return `${year}-${month}-${day}`}}
}
</script>

 效果图:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词