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

相关推荐
kyriewen3 小时前
面试官让我优化一个卡死的表格,我打开 Claude 五秒重写,他放下咖啡问我要不要来当组长
前端·javascript·面试
小粉粉hhh3 小时前
React(二)——dom、组件间通信、useEffect
前端·javascript·react.js
索西引擎5 小时前
【React】key 属性:协调算法中的元素标识机制与最佳实践
前端·javascript·react.js
只会cv的前端攻城狮6 小时前
动态组件架构设计:从配置到事件驱动全链路解析
前端·vue.js
万敏6 小时前
Vue3 全栈实战第二周:Pinia + Vite + Router + axios 完整记录
vue.js·node.js·全栈
OpenTiny社区6 小时前
TinyEngine 2.11版本发布:AI 进一步融入画布,Vue 工程可一键转DSL
前端·vue.js·github
小雪_Snow6 小时前
JavaScript 中的三种常用事件
开发语言·前端·javascript
蓝胖子酱6 小时前
在 Electron 中用 electron-store 实现加密存储与自定义格式文件
前端·javascript·数据库
To_OC8 小时前
LC 22 括号生成:刷完这道题,我终于搞懂回溯剪枝了
javascript·算法·leetcode
小二·8 小时前
Next.js 15 企业级实战:从项目搭建到CI/CD全链路(App Router + Server Components + Turbopack)
javascript·ci/cd·turbopack