Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set minimum and maximum date dynamically in Element-ui date picker

I am having 2 date pickers for startDate and endDate. In startDate Picker,I want to disabled all dates before endDate and vise versa. how to disable dates using elemnt-ui. >

like image 525
user9265584 Avatar asked Jan 29 '18 10:01

user9265584


People also ask

How do I restrict date in date picker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).

How do I set a Datepicker range?

Steps to add Datepicker – $(document). ready(function(){ $("#setMin,#setMax"). datepicker({ dateFormat: "yy-mm-dd" }); $('#datepicker'). datepicker({ dateFormat: "yy-mm-dd", maxDate:'+1m +10d', minDate: -10 }); });

How set MinDate as current date in Datepicker react?

My code: const DatePickerMod = () => { const [startDate, setStartDate] = useState(null); return ( <DatePicker selected={startDate} onChange={date => setStartDate(date)} minDate={'02-01-2020'} maxDate={'02-29-2020} placeholderText="Select a date in February 2020" /> ); }; javascript.

What is a date picker in UI?

Date pickers are UI patterns that allow users to choose a specific date, time, or combination of both–for example, selecting a date of birth. The purpose of these date pickers is to streamline date capture while ensuring format consistency.


3 Answers

This is an old question but I asked myself the same today. You can achieve this using the disabledDate picker option. Here's an example on how to disable all future dates:

<el-date-picker
  :picker-options="datePickerOptions"
</el-date-picker>

Then in your data object:

data () {
  return {
    datePickerOptions: {
      disabledDate (date) {
        return date > new Date()
      }
    }
  }
}
like image 198
Christof Avatar answered Nov 23 '22 17:11

Christof


fist of all you should define picker options for your end date input. The main problem is using this keyword inside the disabledDate method of picker options, so you should move exactly method outside the data definition to the methods part So, complete code should looks something like this:

data () {
    return {
        task: {
            start_at: new Date(),
            end_at: new Date()
        }
        dueDatePickerOptions: {
            disabledDate: this.disabledDueDate
        }
    }
},
methods: {
    disabledDueDate (time) {
        return time.getTime() < this.task.start_at
    },
    validateEndDate () {
        if (this.task.start_at > this.task.due_at) {
            this.task.due_at = this.task.start_at
        }
    }
}

And HTML part of this example should looks like:

<el-date-picker @input="validateEndDate" v-model="task.start_at" type="date"></el-date-picker>
<el-date-picker v-model="task.end_at" :picker-options="dueDatePickerOptions" type="date"></el-date-picker>

Notice: validateEndDate method will be triggered after changing startDate and check if endDate before startDate then fix endDate to be equals.

like image 29
Николай Лантинов Avatar answered Nov 23 '22 18:11

Николай Лантинов


If you want to get between, you can try it:

datePickerOptions: {
  disabledDate: this.disabledDueDate
}

and your method:

methods: {
  disabledDueDate(date) {  // format Date!
    return !(date >= this.start && date <= this.end)
  },
}
like image 40
Shamil Qurbonov Avatar answered Nov 23 '22 17:11

Shamil Qurbonov