element ui - 禁止选择今天后的日期 和日期选择后追加一天

需求,要求选择yyyy-MM-dd格式的组件,但是传参需要yyyy-MM-dd 00:00:00,和禁止选择今天后的日期(包括今天)

这个用的时日期组件,格式为yyyy-MM-dd

复制代码
<el-date-picker v-model="startTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择开始日期" @change="getSales" :picker-options="disabledDateOptions" />

声明变量

disabledDateOptions 的作用是,禁止选择今天以后的日期

复制代码
export default {
  data() {
    return {
      startTime: null,
      endTime: null,
      disabledDateOptions: {
        disabledDate(time) {
          const today = new Date(new Date().setHours(0, 0, 0, 0))
          return time.getTime() >= today.getTime()
        }
      }
    }
  },
  methods: {
    tabList(e) {
      this.tabActive = e
    },
    getSales() {
       if (!this.startTime) {
        this.startTime = null
        this.endTime = null
      } else {
        const date = new Date(this.startTime)
        // 确保 startTime 格式为 "yyyy-MM-dd 00:00:00"
        this.startTime = this.formatDate(date) + ' 00:00:00'
        // 创建新的 Date 对象并加一天作为 endTime
        const endDate = new Date(date)
        endDate.setDate(date.getDate() + 1) // 加一天
        this.endTime = this.formatDate(endDate) + ' 00:00:00'
      }
       console.log(this.startTime,this.endTime )
    },
    // 辅助函数:格式化日期为 "yyyy-MM-dd"
    formatDate(date) {
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0') // 补零
      const day = date.getDate().toString().padStart(2, '0') // 补零
      return `${year}-${month}-${day}`
    }
  }
}

这个如选择 1999-02-02 , this.startTime = 1999-02-02 00:00:00, this.endTime = 1999-02-03 00:00:00

相关推荐
名字还没想好☜33 分钟前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司1 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
心中有国也有家1 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
仙人球部落3 小时前
-python-LangGraph框架(3-31-LangGraph 「合并式状态管理」的原理与实践)
开发语言·javascript·python
sunfdf4 小时前
Next.js 新手从零部署到首跑实战指南
开发语言·javascript·ecmascript
SmartBoyW4 小时前
前端死磕:一文彻底搞懂 JS 事件循环 (Event Loop) 与宏微任务
前端·javascript
不法5 小时前
uniapp map地图导航/地图路线
前端·vue.js·uni-app
mONESY5 小时前
基于MCP协议搭建全链路智能Agent:地图检索+浏览器自动化+本地文件操控实战
javascript
研☆香6 小时前
闭包实战避坑指南
javascript
你怎么知道我是队长6 小时前
JavaScript 的控制语句介绍
开发语言·javascript·ecmascript