The Ignite UI for Web Components Date Picker is a feature rich component used for entering a date through manual text input or choosing date values from a calendar dialog that pops up. Lightweight and simple to use, the Date Picker lets users navigate to a desired date with several view options – month, year, and decade. It also supports common validation properties such as minimum and maximum date constraints and required fields.
The Ignite UI for Web Components Date Picker Component lets users pick a single date through a month-view calendar dropdown or editable input field. The Web Components Date Picker also supports a dialog mode for selection from the calendar only, locale-aware and customizable date formatting and validation integration.
The IgcDatePickerComponent is a brand new component from Ignite UI for Web Components version 5.0.0. The old IgcDatePickerComponent prior to this version has been renamed to XDatePicker and its respective documentation page can be found under "Deprecated Components"
Web Components Date Picker Example
Below you can see a sample that demonstrates how the Date Picker works when users are enabled to pick a date through a manual text input and click on the calendar icon on the left to navigate to it. See how to render it.
EXAMPLE
TS
HTML
CSS
import { defineComponents, IgcDatePickerComponent } from'igniteui-webcomponents';
import'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcDatePickerComponent);
exportclassDatePickerOverview{
constructor() {
let datePicker = document.getElementById('date-picker') as IgcDatePickerComponent;
const date = newDate();
datePicker.value = date;
}
}
new DatePickerOverview();
ts
<!DOCTYPE html><html><head><title>Date Picker Overview</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v5.css"type="text/css" /></head><body><divid="root"><divclass="container sample center"><igc-date-pickerid="date-picker"><pslot="helper-text">Date</p></igc-date-picker></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Like this sample? Get access to our complete Ignite UI for Web Components toolkit and start building your own apps in minutes. Download it for free.
Getting Started with Web Components Date Picker
First, you need to install the Ignite UI for Web Components by running the following command:
npm install igniteui-webcomponents
cmd
You will then need to import the IgcDatePickerComponent, its necessary CSS, and register its module, like so:
The above snippet will add an additional icon at the end of the input, right after the default clear icon. This will not remove the default toggle icon, though as prefixes and suffixes can be stacked one after the other.
Customizing the toggle and clear icons
The calendar and clear icon could be templated by using the calendar and clear slots:
The IgcDatePickerComponent has intuitive keyboard navigation that makes it easy to increment, decrement, or jump through different DateParts among others without having to touch the mouse.
Keys
Description
←
Move one character to the beginning
→
Move one character to the end
Home
Move to the beginning
End
Move to the end
Ctrl / Command + ←
Move to the beginning of the date/time section - current one or left one
Ctrl / Command + →
Move to the end of the date/time section - current on or right one
Focus on a date/time part + ↓
Decrements a date/time part
Focus on a date/time part + ↑
Increments a date/time part
Ctrl / Command + ;
Sets the current date/time as the value of the editor
Esc
Closes the calendar pop-up and focuses the input field
import { defineComponents, IgcDatePickerComponent } from'igniteui-webcomponents';
import'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcDatePickerComponent);
exportclassDatePickerDialogMode{
constructor() {
let datePicker = document.getElementById('date-picker') as IgcDatePickerComponent;
const date = newDate();
datePicker.value = date;
}
}
new DatePickerDialogMode();
ts
<!DOCTYPE html><html><head><title>Date Picker Dialog Mode</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v5.css"type="text/css" /></head><body><divid="root"><divclass="container sample center"><igc-date-pickerid="date-picker"mode="dialog"><pslot="helper-text">Date</p></igc-date-picker></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Display and input format
inputFormat and displayFormat are properties which can be set to make the picker's editor follow a specified format. The inputFormat is locale based, so if none is provided, the picker will default to the one used by the browser.
A good thing to note is that the Date Picker Component will always add a leading zero on the date and month portions if they were provided in a format that does not have it, e.g. d/M/yy becomes dd/MM/yy. This applies only during editing.
import { defineComponents, IgcDatePickerComponent } from'igniteui-webcomponents';
import'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcDatePickerComponent);
exportclassDatePickerFormat{
constructor() {
let datePicker = document.getElementById('date-picker') as IgcDatePickerComponent;
const date = newDate();
datePicker.value = date;
}
}
new DatePickerFormat();
ts
<!DOCTYPE html><html><head><title>Date Picker Display Format</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v5.css"type="text/css" /></head><body><divid="root"><divclass="container sample center"><igc-date-pickerid="date-picker"display-format="shortDate"input-format="dd/MM/yy"></igc-date-picker></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
The IgcDatePickerComponent could be used in a form element, the component's min and max properties act as form validators.
In forms, we can handle the igcChange event of the component and update the value of the label.
EXAMPLE
TS
HTML
CSS
import { defineComponents, IgcButtonComponent, IgcDatePickerComponent } from'igniteui-webcomponents';
import'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcDatePickerComponent, IgcButtonComponent);
exportclassDatePickerOverview{
constructor() {
let form = document.getElementById('form') as HTMLFormElement;
let datePickerValue = document.getElementById('datePickerValue') as HTMLElement;
let formStatus = document.getElementById('formStatus') as HTMLElement;
let datePicker = document.getElementById('datePicker') as IgcDatePickerComponent;
let resetButton = document.getElementById('resetButton') as IgcButtonComponent;
const date = newDate(2024, 4, 15);
const minDate = newDate(date.getFullYear(), date.getMonth(), date.getDate() - 10);
const maxDate = newDate(date.getFullYear(), date.getMonth(), date.getDate() + 15);
datePicker.value = date;
datePicker.max = maxDate;
datePicker.min = minDate;
datePickerValue.innerHTML = `Date picker value: ${datePicker.value ? datePicker.value.toLocaleString() : null}`;
formStatus.innerHTML = `Form valid: ${form.checkValidity()}`;
resetButton.addEventListener('click', this.reset);
datePicker.addEventListener("igcChange", () => {
datePickerValue.innerHTML = `Date picker value: ${datePicker.value ? datePicker.value.toLocaleString() : null}`;
formStatus.innerHTML = `Form valid: ${form.checkValidity()}`;
});
}
publicreset(){
let form = document.getElementById('form') as HTMLFormElement;
let datePickerValue = document.getElementById('datePickerValue') as HTMLElement;
let formStatus = document.getElementById('formStatus') as HTMLElement;
datePickerValue.innerHTML = "Date picker value: ";
formStatus.innerHTML = "Form valid: ";
form.reset();
}
}
new DatePickerOverview();
ts
<!DOCTYPE html><html><head><title>Date Picker Inside A Form</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png"><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v5.css"type="text/css" /></head><body><divid="root"><divclass="container sample center"><formid="form"><igc-date-pickerid="datePicker"required><pslot="helper-text">Date</p></igc-date-picker><div><igc-buttonid="submitButton"onclick="form.requestSubmit()">Submit</igc-button><igc-buttonid="resetButton">Reset</igc-button></div></form><pid="datePickerValue"></p><pid="formStatus"></p></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %>
<scriptsrc="src/index.ts"></script>
<% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Calendar Specific settings
The IgcDatePickerComponent can modify some of the calendar's settings via the properties that the Date Picker exposes. Some of these include visibleMonths which allows more than one calendar to be displayed when the picker expands, weekStart which determines the starting day of the week, showWeekNumbers which shows the number for each week in the year and more.
<!DOCTYPE html><html><head><title>Date Picker Styling</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v5.css"type="text/css" /></head><body><divid="root"><divclass="container sample center"><igc-date-pickermode="dialog"><pslot="helper-text">Date</p></igc-date-picker></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html