Skip to main content

Date Picker
Ready

Date Picker component lets user pick a single date or a date range using a special calendar like date picker interface. There’s also open source version of this component available.

Date Picker allows you to set a minimum and a maximum allowed date. These settings can be combined or used alone, depending on the need. Please note that the date values must be passed in IS0-8601 format: YYYY-MM-DD.

Examples

Open in new window
<duet-date-picker name="date" label="Valitse päivämäärä" placeholder="pp.kk.vvvv" value="2020-06-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker
label="Valitse päivämäärä"
min="2020-06-10"
max="2020-07-18"
value="2020-06-16"
placeholder="pp.kk.vvvv"
>

</duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker
label="Choose date"
language="en"
placeholder="dd.mm.yyyy"
required
value="2020-11-01"
min="2020-10-15"
max="2020-12-15"
>
</duet-date-picker>

<script>
var picker = document.querySelector("duet-date-picker")

function checkValidity() {
if (picker.validity.valid) {
picker.error = ""
} else if (picker.validity.rangeUnderflow) {
picker.error = "Minimum allowed date is: 15.10.2020"
} else if (picker.validity.rangeOverflow) {
picker.error = "Maximum allowed date is: 15.12.2020"
} else if (picker.validity.badInput) {
picker.error = "Please enter a date in the format dd.mm.yyyy"
} else if (picker.validity.valueMissing) {
picker.error = "This field is required"
}
}

picker.addEventListener("duetInput", function () {
checkValidity()
})

picker.addEventListener("duetChange", function (e) {
checkValidity()

if (picker.validity.valid) {
console.log("valid date:", e.detail.value)
}
})
</script>
Open in new window
<duet-fieldset label="Valitse aikaväli">
<duet-grid responsive style="max-width: 480px">
<duet-date-picker
name="startdate"
class="start"
expand
margin="none"
label="Valitse aloituspäivä"
label-hidden
value="2020-06-01"
>
</duet-date-picker>
<duet-spacer direction="horizontal" breakpoint="small" size="x-small"></duet-spacer>
<duet-spacer direction="vertical" breakpoint="x-small" size="small"></duet-spacer>
<duet-icon
class="icon-arrow"
name="navigation-arrow-right"
margin="none"
color="currentColor"
size="x-small"
>
</duet-icon>
<duet-spacer direction="horizontal" breakpoint="small" size="x-small"></duet-spacer>
<duet-spacer direction="vertical" breakpoint="x-small" size="small"></duet-spacer>
<duet-date-picker
name="enddate"
class="end"
expand
margin="none"
label="Valitse lopetuspäivä"
label-hidden
direction="left"
value="2020-06-10"
>
</duet-date-picker>
</duet-grid>
</duet-fieldset>

<style>
.icon-arrow {
transform: rotate(90deg);
align-self: center;
}

@media (min-width: 36em) {
.icon-arrow {
transform: rotate(0);
padding-top: var(--space-medium);
align-self: start;
}
}
</style>

<script>
// Select the above date picker components
var startPicker = document.querySelector(".start")
var endPicker = document.querySelector(".end")
var startDate, endDate

// Handle date selection
function handleDateSelect() {
if (startDate && endDate) {
console.log("date range selected", startDate, endDate)
}
}

// When the start date picker is changed…
startPicker.addEventListener("duetChange", function (e) {
startDate = e.detail.valueAsDate
endPicker.min = e.detail.value

if (!endDate) {
endPicker.show()
}
handleDateSelect()
})

// When the end date date picker is changed…
endPicker.addEventListener("duetChange", function (e) {
endDate = e.detail.valueAsDate
startPicker.max = e.detail.value
handleDateSelect()
})
</script>
Open in new window
<duet-date-picker name="date" expand label="Valitse päivämäärä" placeholder="pp.kk.vvvv" value="2020-06-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker name="date" disabled label="Valitse päivämäärä" value="2020-05-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker name="date" error="There is an error!" label="Choose a date" value="2020-05-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker
tooltip="Hello, I’m a tooltip! To close me you can hit ESC key,
click the close button, or click outside of the tooltip."

label="Choose a date (with tooltip)"
value="2020-05-16"
>

</duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker name="date" language="sv" label="Välj datum" placeholder="dd.mm.åååå" value="2020-06-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Listen for when date is selected
picker.addEventListener("duetChange", function (e) {
console.log("selected date", e.detail.valueAsDate)
})
</script>
Open in new window
<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16"></duet-date-picker>

<script>
// Select the above date picker component
var picker = document.querySelector("duet-date-picker")

// Call show method
setTimeout(function () {
picker.show()
}, 500)
</script>
Open in new window
<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16" caption="caption" echo-placeholder></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16" echo-placeholder></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16" caption="caption"></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" value="2020-06-16"></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" caption="caption" echo-placeholder></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" echo-placeholder></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy" caption="caption"></duet-date-picker>

<duet-date-picker name="date" language="en" label="Choose a date" placeholder="dd.mm.yyyy"></duet-date-picker>

Properties #

Property Attribute Description Type Default
accessibleActiveDescendant accessible-active-descendant Indicates the id of a related component’s visually focused element. string undefined
accessibleControls accessible-controls Use this prop to add an aria-controls attribute. Use the attribute to indicate the id of a component controlled by this component. string undefined
accessibleDescribedBy accessible-described-by Indicates the id of a component that describes the input. string undefined
accessibleOwns accessible-owns Indicates the id of a component owned by the input. string undefined
caption caption Caption (underneath label) that can be set as a way of adding extra information string undefined
direction direction Forces the opening direction of the calendar modal to be always left or right. This setting can be useful when the input is smaller than the opening date picker would be as by default the picker always opens towards right. "left" | "right" "right"
disabled disabled Makes the date picker input component disabled. This prevents users from being able to interact with the input, and conveys its inactive state to assistive technologies. boolean false
echoPlaceholder echo-placeholder If form input field has a placeholder text, and user types anything (causing the text to dissapear), settings this to true will "echo" it into the caption slot - this option will be false by default for the next few versions, but will eventually be true by default (scheduled for 4.30.0) boolean false
error error Display the date picker input in error state along with an error message. string ""
expand expand Expands the date picker input to fill 100% of the container width. boolean false
identifier identifier Adds a unique identifier for the date picker input. string ""
label label Label for the date picker input. string ""
labelHidden label-hidden Visually hide the label, but still show it to screen readers. boolean false
language language [DEPRECATED] this is now handled via the html lang tag, and is no longer used - kept to avoid breaking changes and ease unit testing

The currently active language. This setting changes the month/year/day names and button labels as well as all screen reader labels.
"en" | "fi" | "sv" getLanguage()
margin margin Controls the margin of the component. "auto" | "none" "auto"
max max Minimum date allowed to be picked. Must be in IS0-8601 format: YYYY-MM-DD This setting can be used alone or together with the min property. string ""
min min Minimum date allowed to be picked. Must be in IS0-8601 format: YYYY-MM-DD. This setting can be used alone or together with the max property. string ""
name name Name of the date picker input. string ""
placeholder placeholder Hint text to display before the user types into the date picker input. string getLocaleString({ fi: "pp.kk.vvvv", en: "dd.mm.yyyy", sv: "dd.mm.åååå" })
required required Set whether the input is required or not. Please note that this is necessary for accessible inputs when the user is required to fill them. When using this property you need to also set “novalidate” attribute to your form element to prevent browser from displaying its own validation errors. boolean false
role role Defines a specific role attribute for the date picker input. string undefined
theme theme Theme of the component. "" | "default" | "turva" ""
tooltip tooltip Tooltip to display next to the label of the date picker input. string ""
tooltipDirection tooltip-direction With direction setting you can force the tooltip to always open towards left or right instead of automatically determining the direction. "auto" | "left" | "right" "auto"
validity -- This offers information relating to the current validity of the component. It follow as closely as possible the behaviour of the native date input. Useful properties to inspect are badInput, valueMissing, rangeOverflow, rangeUnderflow. This property should be treated as read only. ValidityState undefined
value value Date value. Must be in IS0-8601 format: YYYY-MM-DD string ""

Events #

Event Description Type
duetBlur Event emitted the date picker input is blurred. CustomEvent<{ component: "duet-date-picker"; }>
duetChange Event emitted when a date is selected. CustomEvent<{ component: "duet-date-picker"; valueAsDate: Date; value: string; }>
duetFocus Event emitted the date picker input is focused. CustomEvent<{ component: "duet-date-picker"; }>

Methods #

hide(moveFocusToButton?: boolean) => Promise<void> #

Hide the calendar modal. Set moveFocusToButton to false to prevent focus
returning to the date picker's button. Default is true.

Returns #

Type: Promise<void>

setFocus(options?: FocusOptions) => Promise<void> #

Sets focus on the date picker's input. Use this method instead of the global focus().

Returns #

Type: Promise<void>

show() => Promise<void> #

Show the calendar modal, moving focus to the calendar inside.

Returns #

Type: Promise<void>


Usage #

This section includes guidelines for designers and developers about the usage of this component in different contexts.

When to use #

  • When the user needs to choose a single date or a date range.

When not to use #

  • For entering date of birth. Use input components instead.
  • To choose a date that is over 10 years in the future or the past.

Accessibility #

This component has been validated to meet the WCAG 2.1 AA accessibility guidelines. You can find additional information regarding accessibility of this component below.

Keyboard support #

Duet Date Picker’s keyboard support is built to closely follow W3C Date Picker Dialog example with some small exceptions to e.g. better support iOS VoiceOver and Android TalkBack.

Choose date button #

Date picker dialog #

Date picker dialog: Month/year buttons #

Date picker dialog: Date grid #

Date picker dialog: Close button #


Integration

For integration, event and theming guidelines, please see Using Components. This documentation explains how to implement and use Duet’s components across different technologies like Angular, React or Vanilla JavaScript.



Tutorials

Follow these practical tutorials to learn how to build simple page layouts using Duet’s CSS Framework, Web Components and other features:

Tutorials

Abstract

Tutorials

Building Layouts

Tutorials

Using CLI Tools

Tutorials

Creating Custom Patterns

Tutorials

Server Side Rendering

Tutorials

Sharing Prototypes

Tutorials

Usage With Markdown

Tutorials

VS Code

Tutorials

Zeplin


Troubleshooting

If you experience any issues while using a component, please head over to the Support page for more guidelines and help.