当前位置: 首页 > news >正文

对当前日期进行按年、按月、按日的取值

 对当前日期进行按年、按月、按日的取值。

其规则为:

按年 为当前日期到来年同一日期的前一天(2024-12-01到2025-11-30)。

按月 为当前日期到下个月的同一日期的前一天 (2024-12-01 到 2024-12-31)。

按日  为当前日期到下一日(2024-12-01 到 2024-12-02)

实验多种方式 ,要么引入组件 要么拆分重组 很麻烦,最后试了个方法觉得挺简便的, 记录下来。

组件用的是vue3的 DatePicker 日期选择器 

      <div style="display: flex; width: 100%; justify-content: space-between"><el-date-pickerv-model="form.StartTime" format="YYYY-MM-DD"value-format="YYYY-MM-DD"type="date"placeholder="开始日期"@change="changeEnterprise"/><div>—</div><el-date-pickerv-model="form.EndTime"format="YYYY-MM-DD"value-format="YYYY-MM-DD"type="date"placeholder="结束日期"disabled/></div>

组件改写选择开始时间,结束时间自动计算。 

   <el-select v-model="form.report" placeholder="请选择时间类型" @change="changeType"><el-option v-for="dict in bus_report_type" :key="dict.value" :label="dict.label" :value="dict.value" />
// 此段代码是字典表取值 转译成假数据<el-option key="0" label="按年" value="0"><el-option key="1" label="按月" value="1"><el-option key="2" label="按日" value="2"></el-select>

 选完时间类型后,显示结束时间。

//根据类型查周期
const changeEnterprise = () => {if (form.value.Type) {const dataTime = new Date(form.value.StartTime);if (form.value.Type == 0) {// 增加一年dataTime.setFullYear(dataTime.getFullYear() + 1);// 减去一天dataTime.setDate(dataTime.getDate() - 1);}if (form.value.Type == 1) {// 增加一个月dataTime.setMonth(dataTime.getMonth() + 1);// 减去一天dataTime.setDate(dataTime.getDate() - 1);}if (form.value.Type == 2) {// 增加一天dataTime.setDate(dataTime.getDate() + 1);}// 格式化日期为 YYYY-MM-DD 字符串const year = dataTime.getFullYear();const month = String(dataTime.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以需要+1,并且补零const day = String(dataTime.getDate()).padStart(2, '0'); // 日期可能也需要补零form.value.EndTime = `${year}-${month}-${day}`;console.log('startDate', dataTime, form.value.EndTime);} else {form.value.Type = null;}
};

附加一个设置 dateTime 的默认值为当前月的第一天到最后一天 

// 获取当前日期
const currentDate = new Date();// 获取当前月的第一天
const firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth());// 获取当前月的最后一天
const lastDay = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);// 将日期格式化为 YYYY-MM-DD
const formatDate = (date) => {const year = date.getFullYear();const month = String(date.getMonth() + 1).padStart(2, '0');const day = String(date.getDate()).padStart(2, '0');return `${year}-${month}-${day}`;
};// 设置 dateTime 的默认值为当前月的第一天到最后一天
const dateTime = ref([formatDate(firstDay), formatDate(lastDay)]);

http://www.lryc.cn/news/521062.html

相关文章:

  • 【Rust自学】12.2. 读取文件
  • C++内存泄露排查
  • Http 响应状态码 前后端联调
  • 48_Lua错误处理
  • shell脚本回顾1
  • 【3】管理无线控制器
  • SOME/IP 协议详解——服务发现
  • Flutter:封装ActionSheet 操作菜单
  • 力扣 全排列
  • Golang 设计模式
  • Matlab 具有周期性分布的死角孔的饱和空气多孔材料的声学特性
  • maven 项目怎么指定打包后名字
  • Java Web开发进阶——Spring Boot与Thymeleaf模板引擎
  • 论文笔记(四十七)Diffusion policy: Visuomotor policy learning via action diffusion(下)
  • 开始使用Panuon开源界面库环境配置并手写VS2019高仿界面
  • 新垂直电商的社交传播策略与AI智能名片2+1链动模式S2B2C商城小程序的应用探索
  • WPS计算机二级•表格函数计算
  • ESP32S3官方例程如何使用
  • 新版 MacOS 无法从 /usr/local/lib 加载动态链接库的解决办法
  • 【Varnish】:解决 Varnish 7.6 CDN 静态资源缓存失效问题
  • 【记录】篡改猴插件下载网页m3u8视频
  • PID控制器 (Proportional-Integral-Derivative Controller) 算法详解及案例分析
  • 【Java设计模式-5】装饰模式:给咖啡加点“佐料”
  • C++ using(八股总结)
  • 《分布式光纤传感:架设于桥梁监测领域的 “智慧光网” 》
  • C++(5)
  • 【进程与线程】程序和进程在内存中的表现
  • 个人主页搭建全流程(Nginx部署+SSL配置+DCDN加速)
  • 语音合成的预训练模型
  • 前端组件开发:组件开发 / 定义配置 / 配置驱动开发 / 爬虫配置 / 组件V2.0 / form表单 / table表单