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
        }
      }
    },
  },
相关推荐
鬼手点金6 分钟前
opencode-全能开发者配置
java·linux·服务器·前端·javascript·学习·前向传播
溪语流沙8 分钟前
【Web全栈进阶】Alembic数据库迁移:改表结构不再删库
前端·数据库·python·elasticsearch·postgresql
AI你一生一世14 分钟前
当构建工具成为基础设施:从 Tailwind 被收购谈起,前端工具链的“归属”难题
前端·构建工具·tailwind css·前端工具链·开源软件收购·技术中立性·前端基础设施
_zxd22 分钟前
TypeScript 类型树
前端·typescript
炸鸡叔42 分钟前
我做了 BotBus:从手机续聊本地 Agent,查看文件和终端
前端·后端
骑着蜗牛撵大象3271 小时前
多 Agent 串行流水线:把一个任务拆成可重试、可续跑的 Pipeline 节点链
前端·langchain
2601_962885721 小时前
AlphaFeed 支持哪些 K 线周期?period参数怎么传?
前端·数据库·python
OpsEye1 小时前
兼顾数据安全与成本,私有化 AI 网关正在成为企业标配
javascript·ai编程
企业数字化笔记1 小时前
AI工具的文件和参数怎么设计?上传校验、配置版本与可复现任务
前端·人工智能