Date Time Picker
Date Time Picker is an input field for selecting both a date and a time.
The date and time can be entered directly using a keyboard in the format of the current locale or through the Date Time Picker’s two overlays. The overlays open when their respective fields are clicked or any input is entered when the fields are focused.
Common Input Field Features
Date Time Picker includes all Text Field and shared input field features.
Step
Date Time Picker’s step parameter defines the time interval (in seconds) between the items displayed in the time picker overlay. It also specifies the amount by which the time increases/decreases using the Up/Down arrow keys (when the overlays are disabled).
The default step is one hour (that is 3600
).
Note that, unlike with Number Field, Date Time Picker accepts values that do not align with the specified step.
new tab
<vaadin-date-time-picker
label="Meeting date and time"
value="2020-06-12T12:30"
.step="${60 * 30}"
></vaadin-date-time-picker>
Note | Use even steps
The step must divide an hour or day evenly.
For example, "15 minutes", "30 minutes" and "2 hours" are valid steps, while "42 minutes" is not.
|
The displayed time format changes based on the step.
new tab
<vaadin-date-time-picker
label="Message received"
value="2020-06-12T15:45:08"
step="1"
></vaadin-date-time-picker>
Step | Format |
---|---|
Less than 60 seconds | HH:MM:SS |
Less than 1 second | HH:MM:SS:FFF |
Note | Limit the number of steps
The overlay does not appear for steps less than 900 seconds (15 minutes) to avoid showing an impractical number of choices.
|
Auto Open
The overlay opens automatically when the field is focused using a pointer (mouse or touch), or when the user types in the field. You can disable that to only open the overlay when the toggle button or Up/Down arrow keys are pressed.
new tab
<vaadin-date-time-picker
label="Meeting date and time"
auto-open-disabled
></vaadin-date-time-picker>
Validation
Min & Max Value
You can define a min and max value for Date Time Picker if you need to restrict the input to a specific range:
new tab
<vaadin-date-time-picker
label="Appointment date and time"
helper-text="Must be within 60 days from today"
.value="${initialValue}"
.min="${minValue}"
.max="${maxValue}"
></vaadin-date-time-picker>
Custom Validation
If the min and max values are not enough, you can also apply custom validation.
new tab
private binder = new Binder(this, AppointmentModel);
firstUpdated() {
this.binder.for(this.binder.model.startDateTime).addValidator({
message: 'The selected day of week is not available',
validate: (startDateTime: string) => {
const date = new Date(startDateTime);
const validWeekDay = date.getDay() >= 1 && date.getDay() <= 5;
return validWeekDay;
},
});
this.binder.for(this.binder.model.startDateTime).addValidator({
message: 'The selected time is not available',
validate: (startDateTime: string) => {
const time = startDateTime.split('T')[1];
const validTime =
(time >= '08:00' && time <= '12:00') || (time >= '13:00' && time <= '16:00');
return validTime;
},
});
}
render() {
return html`
<vaadin-date-time-picker
label="Appointment date and time"
helper-text="Open Mondays-Fridays, 8:00-12:00, 13:00-16:00"
.step="${60 * 30}"
...="${field(this.binder.model.startDateTime)}"
></vaadin-date-time-picker>
`;
}
Week Numbers
You can configure Date Time Picker to show week numbers in the date picker overlay.
new tab
@query('vaadin-date-time-picker')
private dateTimePicker?: DateTimePickerElement;
firstUpdated() {
if (this.dateTimePicker) {
this.dateTimePicker.i18n = {
...this.dateTimePicker.i18n,
firstDayOfWeek: 1,
};
}
}
render() {
return html`
<vaadin-date-time-picker
label="Meeting date and time"
show-week-numbers
></vaadin-date-time-picker>
`;
}
The week numbers are displayed according to ISO-8601. Please note that they can only be displayed when the first day of the week is Monday.
Initial Position
Date Time Picker’s initial position parameter defines which date is focused in the calendar overlay when the overlay is opened. Use this feature to minimise the need for any additional navigation and/or scrolling when the user’s input is expected to be within a certain time frame.
new tab
<vaadin-date-time-picker
label="Meeting date and time"
.initialPosition="${startOfNextMonthISOString}"
></vaadin-date-time-picker>
Usage Patterns
Date Time Range
You can accomplish a date time range picker using two Date Time Pickers.
new tab
<div>
<vaadin-date-time-picker
label="Start date and time"
.value="${this.startDateTime}"
@value-changed="${(e: CustomEvent) => (this.startDateTime = e.detail.value)}"
></vaadin-date-time-picker>
<vaadin-date-time-picker
label="End date and time"
.min="${this.startDateTime}"
.value="${this.endDateTime}"
@value-changed="${(e: CustomEvent) => (this.endDateTime = e.detail.value)}"
></vaadin-date-time-picker>
</div>
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 Practises
Use Date Time Picker when the user needs to choose both a date and time of day. If you only need a date or time of day, use Date Picker and Time Picker respectively.
Picking vs Typing
The calendar overlay is useful when the users need to choose a day that is close to the current date or when information such as day of the week, week number, and valid dates, etc. can aid in choosing the best option.
For days well in the past or future, and for known dates such as birthdays, typing the date can be the faster and easier approach. It is important to verify that the user can enter dates according to their locale.
Input Format
Use a placeholder or helper to show how the input should be formatted. This is important for the date picker if the overlay is disabled and/or when supporting multiple locales. For example, 12/6/2020 does not represent the same date for Americans as it does Europeans.
new tab
<vaadin-date-time-picker
label="Select date and time"
helper-text="Format: DD/MM/YYYY and HH:MM"
date-placeholder="DD/MM/YYYY"
time-placeholder="HH:MM"
></vaadin-date-time-picker>
Helpers are generally preferable to placeholders, as they are always visible, thereby requiring less short-term memory. Fields with placeholders are also less noticeable than empty fields and susceptible to being skipped.
Use placeholders when space is limited, for example, when Date Time Picker is used as a grid filter.