日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

element的DateTimePicker 日期時間選擇器怎么限制只能選擇某個時間段?只能選擇一個月?其它時間不能選擇。

作者:migexiaoliang 更新時間: 2022-08-15 編程語言

項目場景:

我們在使用element的DateTimePicker 日期時間選擇器的時候,有時候會限制范圍,比如只能選擇一個月的時間,這時候需要對選擇的范圍進行限制。

在這里插入圖片描述


實現方案:

我們看element官網我們發現可以用picker-options來對其限制,實現方法如下:

<el-date-picker
   v-model="loggerQueryData.operateTime"
   type="datetimerange"
   start-placeholder="開始時間"
   end-placeholder="結束時間"
   @change="onChangeTime('picker')"
   :picker-options="pickerOptions">
</el-date-picker>
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          let now = new Date();   //獲取此時的時間
          let nowData = new Date(  //獲取此時年月日的后一天
            now.getFullYear(),
            now.getMonth(),
            now.getDate() + 1 //獲取明天
          );
          let oneMonthAgo = new Date(  //獲取一個月之前的時間
            now.getFullYear(),
            now.getMonth() - 1,  //獲取上一個月
            now.getDate() + 1   //將多算的一天減掉
          );
         
          return (
            time.getTime() > nowData.getTime() - 1000  //可以選擇到今天的xxx:xxx:xxx:23:59:59,只有的全部disabled
            || time.getTime() < oneMonthAgo.getTime()  //小于一個月的全部disabled掉
          );
        }
      },
    }
  }
}

效果圖如下:
在這里插入圖片描述

原文鏈接:https://blog.csdn.net/migexiaoliang/article/details/124396906

欄目分類
最近更新