Date Range in Blazor DatePicker Component

8 Aug 20231 minute to read

The 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. The Value property depends on the Min/Max with respect to the StrictMode property. For more information about StrictMode, refer to the Strict Mode section from the documentation.

The following code allows selecting a date within the range from 7th to 27th in a month.

@using Syncfusion.Blazor.Calendars

<SfDatePicker TValue="DateTime?" Min='@MinDate' Max='@MaxDate' Value='@DateValue'></SfDatePicker>

@code {
    public DateTime MinDate {get;set;} = new DateTime(DateTime.Now.Year,DateTime.Now.Month,07);
    public DateTime MaxDate {get;set;} = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 27);
    public DateTime? DateValue {get;set;} = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);
}

Date Selection in Blazor DatePicker

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 indicate the date is out of range or invalid.

@using Syncfusion.Blazor.Calendars

<SfDatePicker TValue="DateTime?" Min='@MinDate' Max='@MaxDate' Value='@DateValue'></SfDatePicker>

@code {
    public DateTime MinDate {get;set;} = new DateTime(DateTime.Now.Year,DateTime.Now.Month,07);
    public DateTime MaxDate {get;set;} = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 27);
    public DateTime? DateValue {get;set;} = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 28);
}

Blazor DatePicker displays Selected Date

NOTE

 If the value of Min or Max properties changed through code behind, you have to update the Value property to set within the range.