User-friendly Datetime Range Picker With Predefined Date Ranges

Category: Date & Time , Javascript | September 29, 2022
Views Total:6,056 views
Official Page:Go to website
Last Update:September 29, 2022


User-friendly Datetime Range Picker With Predefined Date Ranges


This is the vanilla JS version of the jQuery Date Range Picker plugin, which allows you to create a customizable, nice-looking date range picker with pre-defined date ranges.

How to use it:

1. Load the required moment.js and vanilla-datetimerange-picker.js in the document.

<link rel="stylesheet" href="./dist/vanilla-datetimerange-picker.css" />
<script src=""></script>
<script src="./dist/vanilla-datetimerange-picker.js"></script>

2. Create an input field that serves as the trigger for the date range picker.

<input type="text" id="example" />

3. Attach the date range picker to the input field. That’s it.

new DateRangePicker('datetimerange-input1', {
    // options here
}, function (start, end) {
  // callback
  alert(start.format() + " - " + end.format());

4. Available options to customize the date range picker.

new DateRangePicker('example', {
    // parent element
    parentEl: document.body,
    // The start of the initially selected date range
    startDate: moment().startOf('day'),
    // The end of the initially selected date range
    endDate: moment().endOf('day'),
    // The earliest date a user may select
    minDate: false,
    // The latest date a user may select
    maxDate: false,
    // min/max years
    minYear: moment().subtract(100, 'year').format('YYYY');
    maxYear: moment().add(100, 'year').format('YYYY');
    // The maximum span between the selected start and end dates. 
    // Can have any property you can add to a moment object (i.e. days, months)
    dateLimit: false,
    // Hide the apply and cancel buttons, and automatically apply a new date range as soon as two dates or a predefined range is selected
    autoApply: false,
    // Show only a single calendar to choose one date, instead of a range picker with two calendars, the start and end dates provided to your callback will be the same single date chosen
    singleDatePicker: false,
    // Show year and month select boxes above calendars to jump to a specific month and year
    showDropdowns: false,
    // Show localized week numbers at the start of each week on the calendars
    showWeekNumbers: false,
    // Show ISO week numbers at the start of each week on the calendars
    showISOWeekNumbers: false,
    // Display "Custom Range" at the end of the list of predefined ranges, when the ranges option is used. 
    // This option will be highlighted whenever the current date range selection does not match one of the predefined ranges. 
    // Clicking it will display the calendars to select a new range.
    showCustomRangeLabel: true,
    // Allow selection of dates with times, not just dates
    timePicker: false,
    // Use 24-hour instead of 12-hour times, removing the AM/PM selection
    timePicker24Hour: false,
    // Increment of the minutes selection list for times (i.e. 30 to allow only selection of times ending in 0 or 30)
    timePickerIncrement: 1,
    // Show seconds in the timePicker
    timePickerSeconds: false,
    // When enabled, the two calendars displayed will always be for two sequential months (i.e. January and February), and both will be advanced when clicking the left or right arrows above the calendars. 
    // When disabled, the two calendars can be individually advanced and display any month/year.
    linkedCalendars: true,
    // Indicates whether the date range picker should automatically update the value of an  element it's attached to at initialization and when the selected dates change.
    autoUpdateInput: true,
    // Normally, if you use the ranges option to specify pre-defined date ranges, calendars for choosing a custom date range are not shown until the user clicks "Custom Range". When this option is set to true, the calendars for choosing a custom date range are always shown instead.
    alwaysShowCalendars: false,
    // Set predefined date ranges the user can select from. 
    // Each key is the label for the range, and its value an array with two dates representing the bounds of the range
    ranges: {},
    // 'left'/'right'/'center'
    // Whether the picker appears aligned to the left, to the right, or centered under the HTML element it's attached to
    opens: 'right',
    // string: 'down' or 'up' 
    // Whether the picker appears below (default) or above the HTML element it's attached to
    drops: 'down',
    // CSS class names that will be added to all buttons in the picker
    buttonClasses: 'btn btn-sm',
    // CSS class string that will be added to the apply button
    applyButtonClasses: 'btn-primary',
    // CSS class string that will be added to the cancel button
    cancelButtonClasses: 'btn-default',
    // Allows you to provide localized strings for buttons and labels, customize the date display format, and change the first day of week for the calendars
    locale: {
      direction: 'ltr',
      format: moment.localeData().longDateFormat('L'),
      separator: ' - ',
      applyLabel: 'Apply',
      cancelLabel: 'Cancel',
      weekLabel: 'W',
      customRangeLabel: 'Custom Range',
      daysOfWeek: moment.weekdaysMin(),
      monthNames: moment.monthsShort(),
      firstDay: moment.localeData().firstDayOfWeek()

5. API methods.

const instance = new DateRangePicker('example', {})
// set start date
// set end date
// update date ranges

6. Events.

window.addEventListener('apply.daterangepicker', function (ev) {
window.addEventListener('show.daterangepicker', function (ev) {
  // do something
window.addEventListener('hide.daterangepicker', function (ev) {
  // do something
window.addEventListener('showCalendar.daterangepicker', function (ev) {
  // do something
window.addEventListener('hideCalendar.daterangepicker', function (ev) {
  // do something
window.addEventListener('cancel.daterangepicker', function (ev) {
  // do something



  • add a function: updateRanges(Object)


  • Update vanilla-datetimerange-picker.js


  • fixed syntax error: this.element.val() -> this.element.value

You Might Be Interested In:

One thought on “User-friendly Datetime Range Picker With Predefined Date Ranges

Leave a Reply