elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案

没有使用selectableRange 禁用时分秒,是因为他会禁止每天的时分秒。 我们需要解决的是当开始时间、结束时间是同一天时, 开始时间不能超过结束时间。 如果直接清空,用户体验不好。所以用watch监听赋值,当前操作谁,它不满足条件,就改变它本身。

javascript 复制代码
<el-col :span="8">
            <el-form-item label="开始时间" prop="startDate" :rules="globalRules.requiredBlurOrChange">
              <el-date-picker
                v-model="formNew.startDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="{...editStartOptions}"
                placeholder="选择日期"
                @change="changeDate"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="完成时间" prop="endDate" :rules="globalRules.requiredBlurOrChange">
              <el-date-picker
                v-model="formNew.endDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="{...editStopOptions}"
                placeholder="选择日期"
                @change="changeDate"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

 data() {
    return {
    	 editStartOptions: {
        disabledDate: time => {
          if (!this.formNew.endDate) {
            return time.getTime() < new Date(1970 - 1 - 1).getTime();   //禁止选择1970年以前的日期
          } else {
            return time.getTime() > new Date(this.formNew.endDate);
          }
        },
      },
      editStopOptions: {
        disabledDate: time => {
          //开始时间可以和结束时间相同增加
          let startDate= new Date(this.formNew.startDate);
          startDate.setDate(startDate.getDate() - 1);
          return (
            time.getTime() < startDate || time.getTime() < new Date(1970 - 1 - 1).getTime()    //禁止选择1970年以前的日期
          );
        },
      },
    }
}


watch: {
    'formNew.startDate': {
      handler(newVal) {
        if (newVal && this.formNew.endDate && new Date(newVal).getTime() > new Date(this.formNew.endDate).getTime()) {
          this.formNew.startDate = this.formNew.endDate
          console.log(newVal, 'newVal')
        }
      }
    },
    'formNew.endDate': {
      handler(newVal) {
        if (newVal && this.formNew.startDate && new Date(newVal).getTime() < new Date(this.formNew.startDate).getTime()) {
          // this.formNew.startDate = newVal;
          this.formNew.endDate = this.formNew.startDate
        }
      }
    },
  },
相关推荐
七牛云行业应用1 小时前
别每次重复配置了!CLAUDE.md + Hooks 让 Claude Code 开箱就记住你的规则
前端
超人气王1 小时前
新手学前端 JavaScript 类型判断:一篇彻底搞懂 typeof、instanceof 和 Object.prototype.toString
前端·javascript
LucianaiB1 小时前
耗时30天,DocPilot Qwen正式开源:一个免费无广的开源文档 AI 助手
前端·后端
xiaoshuaishuai82 小时前
C# AvaloniaUI 资源找不到报错
java·服务器·前端·windows·c#
丷丩2 小时前
MapLibre GL JS第35课:显示带地形高程(三维地形)的卫星影像
javascript·gis·map·mapbox·maplibre gl js
How_doyou_do2 小时前
26字节工程营-前端-自我总结
前端
三乐2282 小时前
node不认识类型?多半是没用上这几段代码
javascript
十有八七2 小时前
🧩 组件库死亡倒计时?—— AI 编码冲击下的前端基础设施重构
前端·人工智能
风止何安啊2 小时前
我一个前端仔,居然用 Python 搞起了 AI?从零到一,撸了个 AI 聊天框小 demo
前端·人工智能·后端
GISer_Jing2 小时前
Claude Code插件系统全解析
前端·人工智能·ai·架构