Press "Enter" to skip to content

JavaScript Component Datetime Range Picker | daterangepicker

Datetimerange Picker is a JavaScript component that is a date and time range picker, no build needed, no dependencies except for Moment.js, which is inspired by Dan Grossman’s boot data picker.

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

daterangepicker, single calendar date range picker, jquery datepicker date range example, daterangepicker placeholder, date time picker, date range picker two inputs

How to make use of it:

1. Load the required moment.js and vanilla-datetimerange-picker.js within the doc.

<link rel="stylesheet" href="./dist/vanilla-datetimerange-picker.css" />
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="./dist/vanilla-datetimerange-picker.js"></script>

2. Create an input field that serves because the set off for the date range picker.

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

3. Attach the date range picker to the input field.

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

4. Available choices 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()
    },

})

Usage

parametertypemeans
bindElementstring or objectbind element id or bind HTMLElement object
optionsobjectoption set (see Options)
callbackfunction(momemt, moment)change datetime callback, 2 parameters: start and end datetime

Datetime Range Picker, vanilla datetimerange picker Plugin/Github


See Demo And Download

Official Website(alumuko): Click Here

This superior jQuery/javascript plugin is developed by alumuko. For extra Advanced Usages, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *