美文网首页
时间日期选择器禁用当前日期当前时间之前的选择

时间日期选择器禁用当前日期当前时间之前的选择

作者: 扶得一人醉如苏沐晨 | 来源:发表于2023-08-13 10:46 被阅读0次

安装momentjs

npm install moment --save
这里写了三个实践触发selectable,目的是及时更新时间限制
<el-date-picker
                v-model="form.planInTime"
                @focus="selectable"
                @blur="selectable"
                @change="selectable"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择入库时间"
              ></el-date-picker>
  watch: {
    "form.planInTime"() {
      this.selectable();
    },
  },
 pickerOptions: {
        disabledDate() {
          return date.getTime() < new Date().getTime() - 24 * 60 * 60 * 1000;
        },
        // selectableRange 用来限制时分秒的选择,这里要求只能选择当前时间之后到0点的时间点 但应该只限今天
        selectableRange: "00:00:00 - 23:59:59",
      },
// 可选的时间范围
    selectable() {
      const date = moment(this.form.planInTime).startOf("day").format("x");
      const nowDate = moment().startOf("day").format("x");
      // 如果选择的是今天 则需要禁用已经过去的时间节点
      if (date <= nowDate) {
        // 默认选择的最新时间 是当前时间的两分钟后 (留出2分钟的富裕时间)
        this.pickerOptions.selectableRange = `${moment()
          .add(2, "minutes")
          .format("HH:mm:ss")} - 23:59:59`;
      }
      // 如果是以后的日期,则不需要禁用时间节点
      else {
        this.pickerOptions.selectableRange = "00:00:00 - 23:59:59";
      }
    },

相关文章

网友评论

      本文标题:时间日期选择器禁用当前日期当前时间之前的选择

      本文链接:https://www.haomeiwen.com/subject/rzmfmdtx.html