Blazor Date Range Picker Overview
The Ignite UI for Blazor Date Range Picker is a lightweight component that includes a text input and a calendar pop-up, allowing users to easily select start and end dates. It is highly customizable to fit various application requirements, offering features such as date range restrictions, configurable date formats, and more.
Date Range Picker Example
Below is a sample demonstrating the IgbDateRangePicker component in action, where a calendar pop-up allows users to select start and end dates.
Getting Started
To get started with the IgbDateRangePicker component, first we need to register its module as follows:
// in Program.cs file
builder.Services.AddIgniteUIBlazor(typeof(IgbDateRangePickerModule));
You will also need to link an additional CSS file to apply the styling to the IgbDateRangePicker component. The following needs to be placed in the wwwroot/index.html file in a Blazor Web Assembly project or the Pages/_Host.cshtml file in a Blazor Server project:
<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />
Now you can start with a basic configuration of the Blazor IgbDateRangePicker.
For a complete introduction to the Ignite UI for Blazor, read the Getting Started topic.
Usage
The IgbDateRangePicker allows users to select a start and end date either by choosing a date range from a dropdown/calendar pop-up or by typing directly into the input fields - one for the start date and one for the end date. The picker offers two modes for displaying date values: single input and two inputs. Both provide editing and masking capabilities.
When the calendar is visible, a date range can be selected by choosing both a start and end date. Selecting a date will set both the start and end date, and once a second date is chosen, it will set the end date. If a range is already selected, clicking any other date on the calendar will start a new range selection.
Display Date Range Picker
To instantiate a IgbDateRangePicker in its default single input mode, use the following code:
<IgbDateRangePicker @ref="DateRangePicker"></IgbDateRangePicker>
To switch the IgbDateRangePicker to use two inputs, set the UseTwoInputs property to true.
<IgbDateRangePicker UseTwoInputs="true"></IgbDateRangePicker>
Value
In addition to being selected or typed by the user, the range value of the IgbDateRangePicker can also be set using the Value property. It's important to note that the value must follow the format: { start: startDate, end: endDate }, where startDate and endDate are Date objects representing the selected range.
<IgbDateRangePicker @ref="DateRangePicker" Value="@Range" Label="Date Range"/>
@code {
public IgbDateRangePicker DateRangePicker { get; set; }
public IgbDateRangeValue Range = new IgbDateRangeValue()
{
Start = DateTime.Today,
End = DateTime.Today.AddDays(3)
};
}
Read-only & Non-editable
You can also make the IgbDateRangePicker read-only, which disables changing the range value through both typing and calendar selection, disables keyboard navigation, and makes the calendar and clear icons appear visually disabled. This is useful when the range is assigned via the value attribute and is intended to be display-only. To enable this behavior, simply set the ReadOnly property.
<IgbDateRangePicker UseTwoInputs="true" ReadOnly="true"/>
Alternatively, you can use the NonEditable property, which, unlike ReadOnly, only prevents editing the input(s) via typing, while still allowing selection through the calendar and clearing via the clear icon.
<IgbDateRangePicker UseTwoInputs="true" NonEditable="true"/>
Popup modes
By default, when clicked, the IgbDateRangePicker opens its calendar pop-up in dropdown mode. Alternatively, the calendar can be opened in dialog mode by setting the Mode property to dialog.
<IgbDateRangePicker Mode="PickerMode.Dialog"/>
Keyboard Navigation
The IgbDateRangePicker features intuitive keyboard navigation, allowing users to easily increment, decrement, or jump between different component parts, all without needing to use a mouse.
| Keys | Description |
|---|---|
| ← | Moves the caret one character to the left |
| → | Moves the caret one character to the right |
| CTRL + ArrowLeft | Moves the caret to the beginning of the current input mask section or to the start of the previous one if it's already at the beginning |
| CTRL + ArrowRight | Moves the caret to the end of the current input mask section or to the end of the next one if it's already at the end |
| ArrowUp | Increments the currently "focused" part of the input mask by one step |
| ArrowDown | Decrements the currently "focused" part of the input mask by one step |
| HOME | Moves the caret to the beginning of the input mask |
| END | Moves the caret to the end of the input mask |
| CTRL + ; | Sets the current date as the value of the component |
| ALT + ↓ | Opens the calendar dropdown |
| ALT + ↑ | Closes the calendar dropdown |
You can also navigate within the calendar pop-up using the keyboard. The navigation is the same as in the IgbCalendar component.
| Keys | Description |
|---|---|
| ↑ / ↓ / ← / → | Navigates through the days in the month |
| ENTER | Selects the currently focused day |
| PAGE UP | Moves to the previous month's view |
| PAGE DOWN | Moves to the next month's view |
| SHIFT + PAGE UP | Moves to the previous year |
| SHIFT + PAGE DOWN | Moves to the next year |
| HOME | Focuses the first day of the current month that is in view (or earliest month when more than one month view is displayed) |
| END | Focuses the last day of the current month that is in view. (or latest month when more than one month view is displayed) |
| Escape | Closes the calender pop-up |
Layout
Label
You can define a label for the IgbDateRangePicker component using the Label property when it is in single input mode. In two inputs mode, you can use the LabelStart and LabelEnd properties to define labels for the start and end date input fields, respectively.
<IgbDateRangePicker Label="Date Range"/>
<IgbDateRangePicker UseTwoInputs="true" LabelStart="Start Date" LabelEnd="End Date"/>
Format
You also have the option to customize the date format displayed in the input fields. There are three properties available for this purpose: Locale, InputFormat, and DisplayFormat.
The Locale property allows you to set the desired locale identifier, which determines how the date is formatted based on regional conventions.
For example, to display the date in a Japanese format, you can set the locale property like this:
<IgbDateRangePicker Locale="ja-JP"/>
If you want to manually define the date format, you can use the InputFormat property by passing a custom format string:
<IgbDateRangePicker InputFormat="dd/MM/yy"/>
The DisplayFormat property also accepts a custom format string, but it only applies when the input field is idle (i.e., not focused). When the field is focused, the format reverts to the default or to the one defined by InputFormat, if both properties are used together:
<IgbDateRangePicker InputFormat="dd/MM/yy" DisplayFormat='yy/MM/dd'/>
Calendar Layout and Formatting
You can further customize the pop-up calendar using various properties:
| Name | Type | Description |
|---|---|---|
Orientation |
'vertical' or 'horizontal' | Allows you to set whether the calendar should be displayed vertically or horizontally. |
VisibleMonths |
string | Controls how many months are visible at a time, with a value of either 1 or 2. |
ShowWeekNumbers |
string | Enables or disables the week number column in the calendar. |
Open |
boolean | Determines whether the calendar picker is open. |
KeepOpenOnSelect |
boolean | Keeps the calendar picker open after a date selection. |
KeepOpenOnOutsideClick |
boolean | Keeps the calendar picker open when clicking outside of it. |
WeekStart |
string | Sets the start day of the week. |
HideOutsideDays |
boolean | Hides days that fall outside the current month view. |
HideHeader |
boolean | Hides the calendar header (applicable only in dialog mode). |
HeaderOrientation |
'vertical' or 'horizontal' | Aligns the calendar header vertically or horizontally (dialog mode only). |
ActiveDate |
Date | Sets the date that is initially highlighted in the calendar. If not set, the current date becomes the active date. |
<IgbDateRangePicker Orientation="ContentOrientation.Vertical" VisibleMonths="1" ShowWeekNumbers="true"/>
Min & Max
You can also set the Min and Max properties to restrict user input by disabling calendar dates outside the defined range. These properties act as validators, so even if the user manually types a date outside the range, the IgbDateRangePicker will become invalid.
<IgbDateRangePicker Min="@MinDate" Max="@MaxDate"/>
@code {
public DateTime MinDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
public DateTime MaxDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 30);
}
Custom & Predefined Date Ranges
You can also add custom date range chips to the calendar pop-up for faster range selection using the CustomRanges property. For example, you can create a custom date range chip to quickly select the range for the upcoming 7 days, ending with the current date. In addition, by setting the UsePredefinedRanges property, a set of predefined ranges chips will be displayed along with the custom ones.
<IgbDateRangePicker CustomRanges="@CustomRanges" UsePredefinedRanges="true" Label="Custom Ranges" />
@code {
public IgbCustomDateRange[] CustomRanges = [
new IgbCustomDateRange()
{
Label = "Previous 7 Days",
DateRange = new IgbDateRangeValue()
{
Start = DateTime.Today.AddDays(-7),
End = DateTime.Today
}
},
new IgbCustomDateRange()
{
Label = "Next 7 Days",
DateRange = new IgbDateRangeValue()
{
Start = DateTime.Today,
End = DateTime.Today.AddDays(7)
}
}
];
}
Now, when you click the newly created "Next 7 days" chip in the calendar pop-up, the range will automatically be selected, from today through the next 7 days.
Disabled & Special dates
You also have the ability to set disabled dates in the calendar to narrow the range of dates the user can choose from. To set the disabled dates, you can use the DisabledDates property.
<IgbDateRangePicker DisabledDates="@DisabledDates" />
@code {
public IgbDateRangeDescriptor[] DisabledDates = [
new IgbDateRangeDescriptor
{
RangeType = DateRangeType.Between,
DateRange = new DateTime[] { new DateTime(DateTime.Today.Year, DateTime.Today.Month, 5), new DateTime(DateTime.Today.Year, DateTime.Today.Month, 8) },
}];
}
You can see more information about all the possibilities that the DisabledDates property offers here: Disabled dates
You can also do the same if you want to set one or more special dates in the calendar; the only difference is that you need to use the SpecialDates property instead. Special dates
Forms
The IgbDateRangePicker component can also be used seamlessly with the HTML form element. The Min, Max, and Required properties act as form validators.
Additional configuration
Properties
In addition to the properties we've already covered, the IgbDateRangePicker component offers a variety of additional properties that allow you to further configure its behavior.
| Name | Type | Description |
|---|---|---|
Disabled |
boolean | Disables the component. |
NonEditable |
boolean | Disables typing in the input field(s). |
Placeholder |
string | Placeholder text for the single input mode. |
PlaceholderStart |
string | Placeholder text for the start date input (two inputs mode). |
PlaceholderEnd |
string | Placeholder text for the end date input (two inputs mode). |
Outlined |
boolean | Determines whether the input part will have outline appearance in the Material theme. |
Prompt |
string | The prompt character used for unfilled parts of the input(s) mask. |
ResourceStrings |
IgcDateRangePickerResourceStrings | Resource strings for localization of the date-range picker and the calendar. |
Slots
You also have the ability to add custom content and modify the appearance of the IgbDateRangePicker component using the available slots.
The prefix and suffix slots allow you to insert custom content before or after the input field (only available in single input mode):
<IgbDateRangePicker>
<IgbIcon @ref="DropDownIcon" slot="prefix" IconName="dropdown" Collection="material"></IgbIcon>
<IgbIcon @ref="UploadIcon" slot="suffix" IconName="upload" Collection="material"></IgbIcon>
</IgbDateRangePicker>
In two inputs mode, you can use the prefix-start, prefix-end, suffix-start, and suffix-end slots instead to target the individual inputs.
Another set of useful slots are clear-icon and calendar-icon, which allow you to customize the icons for the clear and calendar buttons in the input fields:
<IgbDateRangePicker>
<IgbIcon slot="clear-icon" @ref="ClearIcon" IconName="bin" Collection="material"></IgbIcon>
<IgbIcon slot="calendar-icon" @ref="CalendarIcon" IconName="apps" Collection="material"></IgbIcon>
</IgbDateRangePicker>
In two inputs mode, you can also customize the default “to” text between the fields by using the separator slot:
<IgbDateRangePicker UseTwoInputs="true">
<span slot="separator">till</span>
</IgbDateRangePicker>
The actions slot allows you to insert a custom action button with your own logic. For example, the button below toggles week numbers column in the calendar:
<IgbDateRangePicker Mode="PickerMode.Dialog" @ref="ActionsDateRange">
<IgbButton slot="actions" @onclick="() => ActionsDateRange.ShowWeekNumbers = !ActionsDateRange.ShowWeekNumbers">Toggle week numbers</IgbButton>
</IgbDateRangePicker>
In addition to the slots we've already covered, the following slots are also available in the IgbDateRangePicker component:
| Name | Description |
|---|---|
title |
Renders content for the calendar title. Applicable only in dialog mode. |
helper-text |
Renders content below the input field(s). |
header-date |
Replaces the default current date/range text in the calendar header. Applicable only in dialog mode. |
clear-icon-start |
Custom clear icon for the start input (two inputs mode). |
clear-icon-end |
Custom clear icon for the end input (two inputs mode). |
calendar-icon-start |
Custom calendar icon for the start input (two inputs mode). |
calendar-icon-end |
Custom calendar icon for the end input (two inputs mode). |
calendar-icon-open |
Icon or content shown when the picker is open (applies to both inputs in two inputs mode). |
calendar-icon-open-start |
Icon or content for the open state of the start input (two inputs mode). |
calendar-icon-open-end |
Icon or content for the open state of the end input (two inputs mode). |
Methods
In addition to the properties and slots, the IgbDateRangePicker also exposes few methods that you can use:
| Name | Description |
|---|---|
Show |
Displays the calendar picker component. |
Hide |
Hides the calendar picker component. |
Toggle |
Toggles the calendar picker between the shown and hidden states. |
Clear |
Clears the input fields, removing any user input. |
Select |
Selects a date range value in the picker. |
SetCustomValidity |
Sets a custom validation message. If the provided message is not empty, the input will be marked as invalid. |
Styling
Since the IgbDateRangePicker component uses the IgbCalendar component, it also inherits the Calendar's CSS parts, allowing you to style both components seamlessly. You can find the full list of exposed Calendar CSS parts here: Calendar Styling. In addition to the Calendar's CSS parts, the IgbDateRangePicker also exposes some unique CSS parts that you can use to customize its appearance:
| Name | Description |
|---|---|
separator |
The separator element between the two inputs. |
ranges |
The wrapper that renders the custom and predefined ranges. |
label |
The label wrapper that renders content above the target input. |
container |
The main wrapper that holds all main input elements. |
input |
The native input element. |
prefix |
The prefix wrapper. |
suffix |
The suffix wrapper. |
calendar-icon |
The calendar icon wrapper for the closed state. |
calendar-icon-start |
The calendar icon wrapper for the closed state of the start input (two inputs). |
calendar-icon-end |
The calendar icon wrapper for the closed state of the end input (two inputs). |
calendar-icon-open |
The calendar icon wrapper for the open state. |
calendar-icon-open-start |
The calendar icon wrapper for the open state of the start input (two inputs). |
calendar-icon-open-end |
The calendar icon wrapper for the open state of the end input (two inputs). |
clear-icon |
The clear icon wrapper (single input). |
clear-icon-start |
The clear icon wrapper for the start input (two inputs). |
clear-icon-end |
The clear icon wrapper for the end input (two inputs). |
actions |
The actions wrapper. |
helper-text |
The helper-text wrapper that renders content below the target input. |
igc-date-range-picker::part(label) {
color: var(--ig-gray-600);
}
igc-date-range-picker::part(calendar-icon-start),
igc-date-range-picker::part(calendar-icon-end) {
background-color: var(--ig-primary-500);
color: var(--ig-primary-500-contrast);
}
igc-date-range-picker::part(calendar-icon-open-start),
igc-date-range-picker::part(calendar-icon-open-end) {
background-color: var(--ig-primary-700);
color: var(--ig-primary-700-contrast);
}
igc-date-range-picker::part(clear-icon-start),
igc-date-range-picker::part(clear-icon-end) {
background-color: var(--ig-error-500);
color: var(--ig-error-500-contrast);
}