Date Picker
Input element used to select a date.
Projects /
Input element used to select a date.
The Date Picker component is used to select a date from a calendar.
It is composed of an <input type="text">
element where the user can type the desired date, with the addition of a calendar widget for easier date navigation/selection. The calendar widget can be navigated using the keyboard (arrows to navigate through days, pageup/pagedown to navigate through months).
The Date Picker component comes with the following customizations:
data-months
attribute to the .date-input
element with the comma-separated list of the labels you want to use:<div class="date-input js-date-input" data-months="Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec">
<!-- ... -->
</div>
data-date-format
attribute to the .date-input
element with the new order you want to use:<div class="date-input js-date-input" data-date-format="y-m-d">
<!-- ... -->
</div>
data-date-separator
attribute to the .date-input
element:<div class="date-input js-date-input" data-date-separator="-">
<!-- ... -->
</div>
If your date picker component is created dynamically, you may need to initialize it (once it has been added to the page) to make it work properly.
Once the dynamic content has been added to the page, initialize it using the DatePicker
object:
// do this after your content has been added to the page
var datePicker = document.getElementsByClassName('date-input')[0]; // your dynamically created date picker
new DatePicker({
element: datePicker, // required
// dateFormat: 'y-m-d', // if you want to pass a custom date format
// dateSeparator: '-', // if you want to pass a custom date separator
// months: 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', // if you want ti pass a custom list of month labels
});
If you want to prefill your Date Picker component with a date, use the .js-date-input__text
input element and set its value
equal to the date you want to use. Make sure to use the proper date format (e.g., dd/mm/yyyy).