Date range in Vue Datepicker component
11 Jun 20243 minutes to read
DatePicker provides an option to select a date value within a specified range by using the min
and max
properties. Always the min value has to be lesser than the max value.
When the min and max properties are configured and the selected date value is out-of-range or invalid, then the model value will be set to out of range
date value or null
respectively with highlighted error
class to indicates the date is out of range or invalid.
The value property depends on the min/max with respect to strictMode
property.
The below example allows selecting a date within the range from 7th to 27th day in a month.
<template>
<div id="app">
<div class='wrap'>
<ejs-datepicker id='datepicker' :value='calVal' :min='minVal' :max='maxVal'></ejs-datepicker>
</div>
</div>
</template>
<script setup>
import { DatePickerComponent as EjsDatepicker } from "@syncfusion/ej2-vue-calendars";
var today = new Date();
var currentYear = today.getFullYear();
var currentMonth = today.getMonth();
const calVal = new Date(new Date().setDate(14));
const minVal = new Date(currentYear, currentMonth, 7);
const maxVal = new Date(currentYear, currentMonth, 27);
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrap {
margin: 35px auto;
width: 240px;
}
</style>
<template>
<div id="app">
<div class='wrap'>
<ejs-datepicker id='datepicker' :value='calVal' :min='minVal' :max='maxVal'></ejs-datepicker>
</div>
</div>
</template>
<script>
import { DatePickerComponent } from "@syncfusion/ej2-vue-calendars";
export default {
name: "App",
components: {
"ejs-datepicker": DatePickerComponent
},
data() {
var today = new Date();
var currentYear = today.getFullYear();
var currentMonth = today.getMonth();
return {
calVal: new Date(new Date().setDate(14)),
minVal: new Date(currentYear, currentMonth, 7),
maxVal: new Date(currentYear, currentMonth, 27),
}
}
}
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import "../node_modules/@syncfusion/ej2-vue-calendars/styles/material.css";
.wrap {
margin: 35px auto;
width: 240px;
}
</style>
If the value of
min
ormax
properties changed through code behind, then you have to update thevalue
property to set within the range.