SoFunction
Updated on 2025-04-05

Elementui Time/Date Selector Selection Disable the current before (after) time code instance

01. Date selection

<template>
  <div>
    <el-date-picker
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      type="date"
      placeholder="Please select a date"
      v-model="dayDate"
      :picker-options="setDisabled"
    ></el-date-picker>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dayDate: "",
      setDisabled: {
        // Return to the disabled time        disabledDate(time) {
          return () &gt; ();  // Select historical sky, current sky, and unselectable future sky          // return () > () - 8.64e7; // Select historical sky, not current sky, not future sky          // return () < () - 8.64e7; // Cannot select historical sky, current sky, future sky          // return () < (); // Cannot select historical sky, current sky, future sky        },
      },
    };
  },
  /*
     8.64e7 is the scientific counting method. Multiply 8.64 by 10 to the power of 7, which is 86400000 that is 1000*60*60*24, which is the number of milliseconds in a day.  because()
     The method can return the number of milliseconds obtained from 00:00:00 (UTC) on January 1, 1970 to the current time.  We are the time zone of Beijing time, which is East 8.
     The starting time corresponds to: "1970/01/01 08:00:00"

     picker-options requires a final boolean value, so whether to subtract 8.64e7 is whether to move forward one day, that is, whether to include the current number of days
   */
};
&lt;/script&gt;

02. Month selection (example)

&lt;template&gt;
    &lt;div&gt;
        &lt;el-date-picker
          v-model="value"
          type="monthrange"
          value-format="yyyy-MM"
          format="yyyy-MM"
          :picker-options="setMonthDisabled"
          range-separator="to"
          start-placeholder="Start Time"
          end-placeholder="End Time"&gt;
        &lt;/el-date-picker&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
    data() {
        return {
            value: "",
            setMonthDisabled: {
              // Return to the disabled time              disabledDate(time) {
                // Get current month information                const date = new Date(); // Get the basic information of the current time, the value is as follows: Tue Jul 20 2021 14:59:43 GMT+0800 (China Standard Time)                const year = (); // Get the current year, the value is as follows: 2021                let month = () + 1; // Get the current month, the value is as follows: 6 getMonth() method returns the value from 0-11, that is, from January to December, so 1 must be added to be the current month.                if (month &gt;= 1 &amp;&amp; month &lt;= 9) {
                  // If it is from January to September, you should add a 0 in the front. For example: 02 and 07 months are expressed in this way.                  month = "0" + month;
                }
                const nowDate = () + (); // Convert to string splicing, and finally get year and month. For example, the time is July 20, 2021, so the value of nowDate is: 202107      
                // Get the month information of the time selector                const timeyear = (); // Get the year of the time selector (there are many)                let timemonth = () + 1; // Same as above                if (timemonth &gt;= 1 &amp;&amp; timemonth &lt;= 9) {
                  timemonth = "0" + timemonth;
                }
                const elTimeData = () + ();
      
                // Return, the date month of the time selector is larger than the month of the current date, and because the disabledDate function controls the month and is not selectable                // So, in the end, it is: the month in the time selector is larger than the current month, so it is disabled, so the final effect is achieved:                // Nothing less than or equal to the current month is not selected                return elTimeData &lt;= nowDate; // Although it is a string here, the weak-type language js will do a conversion, which can be compared with the size, such as: '202107' > '202008'              },
            },    
        }
    }
}
&lt;/script&gt;

Attachment: element ui date-picker disable dates after today

&lt;el-date-picker
       v-bind="$attrs"
       v-on="$listeners"
       placeholder="Select a date"
       :picker-options="pickerOptions"&gt;
        v-model='dateVal'
&lt;/el-date-picker&gt;
 
 
//Disable today and later dates pickerOptions: {
        disabledDate(time) {
          return () &gt; () - 24 * 3600 * 1000
        }
 },
 
//Disable date after today pickerOptions: {
        disabledDate(time) {
          return () &gt; ()
        }
 },

Summarize

This is the article about the elementui time/date selector to disable the current (after) time. For more related contents of the elementui date selector to disable the time, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!