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

相关推荐
Lao乾妈官方认证唯一女友:D1 小时前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链
UI设计兰亭妙微1 小时前
飞秒激光时域热反射测量系统软件UI界面设计
ui·ui设计公司
ALKAOUA1 小时前
力扣面试150题刷题分享
javascript·笔记
少云清1 小时前
【UI自动化测试】1_IOS自动化测试 _环境搭建
ui·ios
迪巴拉15251 小时前
基于Vue与Spring Boot+Open Cv的智慧校园考勤系统
前端·vue.js·spring boot
swipe2 小时前
JavaScript 对象与属性描述符:从原理到实战
前端·javascript·面试
&活在当下&2 小时前
Vue3 h函数用法详解
前端·javascript·vue.js
小贵子的博客2 小时前
(vue3错误处理)has naming conflicts with other components, ignored.
前端·javascript·vue.js