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

相关推荐
光影少年11 小时前
前端算法题
前端·javascript·算法
漓漾li12 小时前
每日面试题(2026-05-15)- 前端
前端·vue.js·react.js
前端那点事13 小时前
告别低级冗余!10个前端原生高阶技巧,让代码更优雅、性能更出众
前端·vue.js
hexu_blog13 小时前
前端vue后端java如何实现证件照功能
前端·javascript·vue.js
豹哥学前端13 小时前
前端 LocalStorage 实战:从入门到熟练,一篇就够了
前端·javascript·面试
Southern Wind13 小时前
谷记账——一个 Vue 3 批次记账 App
前端·javascript·vue.js
A923A13 小时前
【javaScript 原型精讲】
javascript·原型·原型链
AITOP10013 小时前
高德联合千问开源AGenUI:让Agent UI同时跑在iOS、安卓和鸿蒙上
ui·ios·开源
卷帘依旧13 小时前
手写throttle
javascript
伯远医学14 小时前
Nat. Methods | 邻近标记技术:活细胞中捕捉分子互作的新利器
java·开发语言·前端·javascript·人工智能·算法·eclipse