Documentation

Documentation versions (currently viewingVaadin 23)

You are viewing documentation for Vaadin 23. View latest documentation

Date Picker

Date Picker is an input field that allows the user to enter a date by typing or by selecting from a calendar overlay.

Open in a
new tab
Source code
date-picker-basic.ts
DatePickerBasic.java

The date can be entered directly using the keyboard in the format of the current locale or through the date picker overlay. The overlay opens when the field is clicked and/or any input is entered when the field is focused.

Common Input Field Features

Date Picker includes all Text Field and shared input field features.

Date Format

Date Picker can be configured to display dates and parse user input in a specific format.

Using Java Locales (Java Only)

By default, Date Picker displays and parses dates using the user’s locale (reference). Alternatively, setting a specific locale ensures that all users consistently see the same format.

Open in a
new tab
Source code
DatePickerLocale.java

The date format that’s used based on the locale depends on the specific browser implementation and might not be reliable when expecting a specific pattern. For finer-grained control over the date format, see the next section.

Using Custom Date Formats (Java Only)

Date Picker allows you to configure a custom date format pattern that defines exactly how dates should be displayed and how user input should be parsed. Additional parsing formats can be provided to support entering dates in different formats. Parsing is first tried with the primary format, then by additional parsing formats in the order that they are provided.

Open in a
new tab
Source code
DatePickerCustomFormat.java

A custom date format pattern is a string that consists of specific symbols that specify how and where a part of the date (day, month, or year) should be displayed. The following symbols are recognized as parts of the date in a pattern:

d

Day of the month, as one or two digits

dd

Day of the month, padded to two digits

M

Month, as one or two digits

MM

Month, padded to two digits

yy

Year, using two digits

yyyy

Year, using four digits

Other characters, such as separators (for example /, ., -) or spaces may be used in a pattern.

Examples:

Pattern Example value Description

M/d/yyyy

8/26/2021

United States date format

yyyy-MM-dd

2021-08-26

ISO 8601 date format

d. M. yyyy

26. 8. 2021

Croatian date format using spaces

M/d

8/26

Date format using only day and month

Custom date patterns take precedence over the configured locale. When using both at the same time, the custom date pattern is used to display and parse dates.

Two-Digit Year Formats

Date Picker supports date formats with 2-digit years (such as dd.MM.yy). When parsing a 2-digit year, the component must decide which century to parse the year as. For example, a year entered as 62 could be interpreted either as 62 AD, 1962, or 2062.

Two-digit years are interpreted as being within a 100-year range whose midpoint is called the reference date. For example, a reference date of 2000-01-01 means that the date entered is interpreted as being within the range 1950-01-01 – 2049-12-31, and the year 50 is parsed to 1950 while 49 is parsed to 2049.

The reference date defaults to the current date, but you can set it to a different date with the internationalization API:

Source code
Java
DatePicker.DatePickerI18n i18n = new DatePicker.DatePickerI18n();
i18n.setDateFormat("yy-MM-dd");
i18n.setReferenceDate(LocalDate.of(1980, 2, 2));
datePicker.setI18n(i18n);
TypeScript

When using a display format with a 4-digit year, years prior to 1000 are displayed padded with leading zeroes to avoid ambiguity.

Note
Display formats with 2-digit years should only be used with 2-digit parsing formats
Using a display format with 2-digit years (yy) together with parsing formats with 4-digit years (yyyy) can cause unexpected behavior, and should be avoided.

Using Custom Functions (Web Component Only)

When using the web component standalone, custom functions can be configured to display and parse the date.

Open in a
new tab
Source code
date-picker-custom-functions.ts
Note
Third-party library
The previous example uses the third-party library date-fns to implement the custom formatting and parsing functions. It needs to be added as a separate dependency to the project.

Validation

Min and Max Value

The valid input range of Date Picker can be restricted by defining min and max values. Dates before the min and after the max are disabled in the overlay. A helper text can be used to inform the user about the accepted range.

Open in a
new tab
Source code
date-picker-min-max.ts
DatePickerMinMax.java

Custom Validation

Date Picker supports custom validation, such as limiting the options to Monday through Friday.

Open in a
new tab
Source code
date-picker-custom-validation.ts
DatePickerCustomValidation.java

Week Numbers

Week numbers (ISO-8601) can be enabled in the calendar overlay. This works only when the first day of the week is set to Monday.

Open in a
new tab
Source code
date-picker-week-numbers.ts
DatePickerWeekNumbers.java

Initial Position

Date Picker’s initial position parameter defines which date is focused in the calendar overlay when the overlay is opened. The default initial position is the selected or current date.

Use this feature to minimize the need for unnecessary navigation and/or scrolling when the user’s input is expected to be within a certain time.

Open in a
new tab
Source code
date-picker-initial-position.ts
DatePickerInitialPosition.java

Auto Open

The overlay automatically opens when the field is focused with a click or a tap, and when typing a value in the input. This can be prevented, to have the overlay only open when the toggle button or the up/down arrow keys are pressed. The behavior isn’t affected on touch devices.

Open in a
new tab
Source code
date-picker-auto-open.ts
DatePickerAutoOpen.java

Internationalization (i18n)

Date Picker allows localizing texts and labels, such as month names and button labels.

Open in a
new tab
Source code
date-picker-internationalization.ts
DatePickerInternationalization.java

Usage Patterns

Date Range

You can create a date range picker using two Date Pickers.

Open in a
new tab
Source code
date-picker-date-range.ts
DatePickerDateRange.java

To disable the days before the start date in the end date picker, you need to handle the selection in the start date picker and change the range in the end date picker.

Best Practices

Picking vs Typing

The calendar overlay is useful when the users need to choose a day that’s close to the current date or when information such as day of the week, week number, and valid dates, and so on, can aid in choosing the best option.

For days well into the past or future, and for known dates such as birthdays, typing the date in the input field can be a faster and easier approach. Because of this, it’s important to verify that the user can enter dates according to their locale.

Instead of a Date Picker, you can use individual input fields for day, month, and year, to improve usability on small touch devices.

Open in a
new tab
Source code
date-picker-individual-input-fields.ts
DatePickerIndividualInputFields.java
Note
Not production-ready
The previous example is only a prototype implementation to demonstrate the idea, and isn’t ready for production use.

Show the Date Format

Use a placeholder or helper to show how the input should be formatted. For example, "12/6/2020" represents different dates for Americans and Europeans.

Open in a
new tab
Source code
date-picker-date-format-indicator.ts
DatePickerDateFormatIndicator.java

Helpers are preferable to placeholders, as they are always visible. Fields with placeholders are also less noticeable than empty fields, so they are susceptible to being skipped. Use placeholders when space is limited, for example when Date Picker is used as a filter in a data grid header.

Component Usage recommendations

Time Picker

Input field for entering or selecting a specific time.

Date Time Picker

Input field for selecting both a date and a time.

26595CB7-1A81-4EE1-B94C-948E889C3027