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

相关推荐
tangdou3690986559 分钟前
AI真好玩系列-2分钟快速了解DeepAgents | Quick Guide to DeepAgents in 2 Minutes
前端·javascript·后端
张元清10 分钟前
React useIntersectionObserver Hook:懒加载与可见性检测(2026)
javascript·react.js
彭于晏爱编程20 分钟前
纯 JS + Node,一个下午手搓了能读懂公司代码的 AI 助手,老板以为我转行了
前端·javascript
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十四):眨眼小人登录页制作
前端·javascript·ai编程
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十三):前端路由初始化
前端·javascript·ai编程
PBitW2 小时前
GPT训练我的第四天,被打惨了!!!😭😭😭
前端·javascript·面试
DarkLONGLOVE2 小时前
快速上手 Pinia!Vue3 极简状态管理使用教程
javascript·vue.js
mackbob2 小时前
.eslintrc.js详细配置说明
javascript
宸翰3 小时前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
用户298698530143 小时前
在 React 中使用 JavaScript 将 Excel 转换为 PDF
javascript·react.js·webassembly