Skip to main content
Yoummday Light Light Dark System

Datepicker

<sl-datepicker> | SlDatepicker
Since 2.41.0 stable Not in big bundle

Datepicker with range and datetime support.

Extends SlInput and therefore inherits properties, events and methods from it except they were intentionally overriden and marked as private.

Built on top of flatpickr. Inspired by lit-flatpickr.





                
Serialize form
<form id="demo">
  <sl-datepicker placeholder="English Date" name="single-en"></sl-datepicker>
  <br>
  <sl-datepicker locale="de" placeholder="German Date" name="single-de"></sl-datepicker>
  <br>
  <label>Prefilled</label>
  <sl-datepicker value="2025-07-21" name="prefilled"></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('demo');
</script>

Examples

Date range





                
Serialize form
<form id="range-demo">
  <sl-datepicker placeholder="Range with array value" mode="range" name="boringRange"></sl-datepicker>
  <br>
  <sl-datepicker placeholder="Range with separate key value pairs" mode="range" name="range[from]" additionalName="range[to]"></sl-datepicker>
  <br>
  <label>Prefilled</label>
  <sl-datepicker mode="range" value="2025-07-21 2025-07-26" name="other[from]" additionalName="other[to]"></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('range-demo');
</script>

Week select




                
Serialize form
<form id="week-demo">
  <sl-datepicker placeholder="Select a week" name="week" week-select></sl-datepicker>
  <br>
  <sl-datepicker placeholder="Week with separate key value pairs" name="from" additionalName="to" week-select></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('week-demo');
</script>

Clearable





                
Serialize form
<form id="clear-demo">
  <sl-datepicker placeholder="Select a week" name="week" week-select clearable></sl-datepicker>
  <br>
  <sl-datepicker placeholder="Select a day" name="day" clearable></sl-datepicker>
  <br>
  <sl-datepicker placeholder="Select a range" name="range" mode="range" clearable></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('clear-demo');
</script>

Disabled

<sl-datepicker placeholder="Forget it!" disabled></sl-datepicker>

Min date / Max date



<sl-datepicker class="minmax" placeholder="Choose future"></sl-datepicker>
<br>
<sl-datepicker class="minmax" placeholder="Choose past"></sl-datepicker>
<br>
<sl-datepicker class="minmax" placeholder="Choose "></sl-datepicker>

<script>
// instead of calculating dates and setting them programmatically, just use attributes like min="YYYY-MM-DD"
  window.addDays = (date, days) => {
    const result = new Date(date);
    result.setDate(result.getDate() + days);
    return result;
  }
  const pickers = document.getElementsByClassName('minmax');
  const today = new Date();
  const todayStr = today.toISOString().split('T')[0];
  const dayAfterTomorrow = addDays(today, 2).toISOString().split('T')[0];
  const dayBeforeYesterday = addDays(today, -2).toISOString().split('T')[0];
  pickers[0].setAttribute('min', todayStr);
  pickers[1].setAttribute('max', todayStr);
  pickers[2].setAttribute('min', dayBeforeYesterday);
  pickers[2].setAttribute('max', dayAfterTomorrow);
</script>

Formatting

flatpickr formatting tokens




                
Serialize form
<form id="format-demo">
  <sl-datepicker name="default" placeholder="Visible: 'F j, Y'; Internal: 'Y-m-d'"></sl-datepicker>
  <br>
  <sl-datepicker name="fancy-to-ISO" placeholder="Visible: 'F j, Y'; Internal: 'Z'" altFormat="J of F \\i\\n t\\he \\year Y" dateFormat="Z"></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('format-demo');
</script>

Date with Time

flatpickr formatting tokens



                
Serialize form
<form id="datetime-demo">
  <sl-datepicker name="datetime" enableTime altFormat="F j, Y | H:i" dateFormat="Y-m-d H:i:S"></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('datetime-demo');
</script>

Time only



                
Serialize form
<form id="time-demo">
  <sl-datepicker name="time" mode="time" dateFormat="H:i" altFormat="H:i" enableTime noCalendar></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('time-demo');
</script>

Datetime range




                
Serialize form
<form id="datetime-range-demo">
  <sl-datepicker placeholder="Range with time" mode="range" name="datetimeRange" enableTime dateFormat="Y-m-d H:i:S" altFormat="F j, Y | H:i"></sl-datepicker>
  <br>
  <label>Prefilled</label>
  <sl-datepicker id="prefilled-datetime-range" mode="range" name="datetimeRange-prefilled" enableTime dateFormat="Y-m-d H:i:S" altFormat="F j, Y | H:i"></sl-datepicker>
  <br>
  <pre></pre>
  <br>
  <sl-button type="submit">Serialize form</sl-button>
</form>

<script type="module">
  makeFormDemo('datetime-range-demo');
  // instead of calculating dates and setting them programmatically, just use a value attribute like so: value="fromDatetime toDatetime" (in the format of dateFormat)
  const picker = document.getElementById('prefilled-datetime-range');
  let today = new Date(Date.now());
  today.setMinutes(today.getMinutes() + 30);
  today.setMinutes(0, 0, 0);
  const todayStr = today.toISOString().split('T').join(' ').split('.')[0];
  const dayAfterTomorrow = addDays(today, 2).toISOString().split('T').join(' ').split('.')[0];
  picker.value = `${todayStr} ${dayAfterTomorrow}`;
</script>

Position

Flatpickr option static is internally set to true, so it is rendered in the shadowDom of the assigned element.
This unfortunately makes position: auto (calculating offset to viewport boundaries) impossible, but we can still set definite values.




<sl-datepicker placeholder="Default"></sl-datepicker>
<br>
<sl-datepicker placeholder="Above" position="above"></sl-datepicker>
<br>
<sl-datepicker placeholder="Above Right" position="above right"></sl-datepicker>
<br>
<sl-datepicker placeholder="Right" position="right"></sl-datepicker>

Events



              

              
<sl-datepicker id="event-picker" placeholder="Play with me" position="above" clearable></sl-datepicker>
<pre id="event-output"></pre>

<script type="module">
  // instead of addingEventListeners, in a lit environment, use @eventName on the element
  const picker = document.getElementById('event-picker');
  const output = document.getElementById('event-output');
  'sl-change sl-input sl-focus sl-blur sl-clear sl-invalid'.split(' ').forEach((t) => {
    picker.addEventListener(t, (e) => {
      output.textContent = `${output.textContent}\n${e.type} -- ${e.currentTarget.value || 'no value'} `
    })
  })
</script>

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://v2-48-10.yoummday-theme.pages.dev/components/datepicker/datepicker.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://v2-48-10.yoummday-theme.pages.dev/components/datepicker/datepicker.js';

To import this component using a bundler:

import '@yoummday/ymmd-shoelace/dist/components/datepicker/datepicker.js';

To import this component as a React component:

import SlDatepicker from '@yoummday/ymmd-shoelace/dist/react/datepicker';

Slots

Name Description
label The input’s label. Alternatively, you can use the label attribute.
prefix Used to prepend a presentational icon or similar element to the input.
suffix Used to append a presentational icon or similar element to the input.
clear-icon An icon to use in lieu of the default clear icon.
show-password-icon An icon to use in lieu of the default show password icon.
hide-password-icon An icon to use in lieu of the default hide password icon.
help-text Text that describes how to use the input. Alternatively, you can use the help-text attribute.

Learn more about using slots.

Properties

Name Description Reflects Type Default
value The current internal value(s) of the picker, submitted as name/value pair with form data. When mode is range or weekSelect is true, the value attribute will be a space-delimited list of values and the value property will be an array. string ''
additionalName An additional name for an additional (hidden) input when mode is range, submitted as a name/value pair with form data. string ''
validity Gets the validity state object - -
altFormat Exactly the same as date format, but for the altInput field string 'F j, Y'
allowInput Allows the user to enter a date directly input the input field. By default, direct entry is disabled. boolean true
ariaDateFormat Defines how the date will be formatted in the aria-label for calendar days, using the same tokens as dateFormat. If you change this, you should choose a value that will make sense if a screen reader reads it out loud string 'F j, Y'
dateFormat A string of characters which are used to define how the date will be displayed in the input box. string 'Y-m-d'
defaultDate Sets the initial selected date(s). If you’re using mode: “multiple” or a range calendar supply an Array of Date objects or an Array of date strings which follow your dateFormat. Otherwise, you can supply a single Date object or a date string. DateOption|DateOption[] -
defaultHour Initial value of the hour element. number 12
defaultMinute Initial value of the minute element. number 0
disableDates Dates selected to be unavailable for selection. DateLimit[] []
disableMobile Set disableMobile to true to always use the non-native picker. By default, flatpickr utilizes native datetime widgets unless certain options (e.g. disable) are used. boolean false
enable Dates selected to be available for selection. DateLimit[] undefined
enableTime Enables time picker boolean false
enableSeconds Enables seconds in the time picker boolean false
hourIncrement Adjusts the step for the hour input (incl. scrolling) number 1
minuteIncrement Adjusts the step for the minute input (incl. scrolling) number 5
inline Displays the calendar inline boolean false
mode “single”, “multiple”, “time” or “range” "single" | "multiple" | "range" 'single'
noCalendar Hides the day selection in calendar. Use it along with enableTime to create a time picker. boolean false
position Where the calendar is rendered relative to the input "above" | "below" 'below'
shorthandCurrentMonth Show the month using the shorthand version (ie, Sep instead of September) boolean false
showMonths The number of months showed number 1
time_24hr Displays the time picker in 24 hour mode without AM/PM selection when enabled boolean true
weekNumbers Enabled display of week numbers in calendar boolean true
name The name of the input, submitted as a name/value pair with form data. string ''
defaultValue The default value of the form control. Primarily used for resetting the form control. string ''
size The input’s size. 'small' | 'medium' | 'large' 'medium'
filled Draws a filled input. boolean false
pill Draws a pill-style input with rounded edges. boolean false
label The input’s label. If you need to display HTML, use the label slot instead. string ''
helpText
help-text
The input’s help text. If you need to display HTML, use the help-text slot instead. string ''
clearable Adds a clear button when the input is not empty. boolean false
disabled Disables the input. boolean false
placeholder Placeholder text to show as a hint when the input is empty. string ''
readonly Makes the input readonly. boolean false
form By default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ''
required Makes the input a required field. boolean false
pattern A regular expression pattern to validate input against. string -
minlength The minimum length of input that will be considered valid. number -
maxlength The maximum length of input that will be considered valid. number -
min The input’s minimum value. Only applies to date and number input types. number | string -
max The input’s maximum value. Only applies to date and number input types. number | string -
autocapitalize Controls whether and how text input is automatically capitalized as it is entered by the user. 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters' -
autocorrect Indicates whether the browser’s autocorrect feature is on or off. 'off' | 'on' -
autocomplete Specifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values. string -
autofocus Indicates that the input should receive focus on page load. boolean -
enterkeyhint Used to customize the label or icon of the Enter key on virtual keyboards. 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' -
spellcheck Enables spell checking on the input. boolean true
inputmode Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices. 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url' -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sl-blur onSlBlur Emitted when the control loses focus. -
sl-change onSlChange Emitted when an alteration to the control’s value is committed by the user. -
sl-clear onSlClear Emitted when the clear button is activated. -
sl-focus onSlFocus Emitted when the control gains focus. -
sl-input onSlInput Emitted when the control receives input. -
sl-invalid onSlInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

Learn more about events.

Methods

Name Description Arguments
focus() Sets focus on the input. options: FocusOptions
blur() Removes focus from the input. -
select() Selects all the text in the input. -
setSelectionRange() Sets the start and end positions of the text selection (0-based). selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'
setRangeText() Replaces a range of text with a new string. replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' | 'preserve'
showPicker() Displays the browser picker for an input element (only works if the browser supports it for the input type). -
stepUp() Increments the value of a numeric input type by the value of the step attribute. -
stepDown() Decrements the value of a numeric input type by the value of the step attribute. -
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. Pass an empty string to restore validity. message: string

Learn more about methods.

Parts

Name Description
form-control The form control that wraps the label, input, and help text.
form-control-label The label’s wrapper.
form-control-input The input’s wrapper.
form-control-help-text The help text’s wrapper.
base The component’s base wrapper.
input The internal <input> control.
prefix The container that wraps the prefix.
clear-button The clear button.
password-toggle-button The password toggle button.
suffix The container that wraps the suffix.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <sl-icon>