Press "Enter" to skip to content

Date Range, Date and Time Picker JavaScript Component

The date range picker component creates a dropdown list from which the user can select a range of dates. I created it while building the UI for Improvely, which needed a way to define date ranges for reports.

Features include selectable date range limit, translatable strings and date formats, single date picker mode, time picker, and preset time ranges.

jquery datepicker date range example, long bill jquery date range picker, jquery responsive date range picker, jquery time range picker, jquery datepicker select week range

How to make use of it:

1. Include the required jQuery library and moment.js on the web page.

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="/path/to/cdn/moment.min.js"></script>

2. Include the Daterangepicker plugin’s information.

<link rel="stylesheet" href="daterangepicker.css" />
<script src="daterangepicker.js"></script>

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

<input type="text" name="basic" id="basic" value="08/01/2021 - 08/27/2021" />
$(function(){
  $('#basic').daterangepicker();
});

4. Create your individual predefined date ranges.

$('#myInput').daterangepicker({
  ranges: {
     'Today': [moment(), moment()],
     'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
     'Last 7 Days': [moment().subtract(6, 'days'), moment()],
     'Last 30 Days': [moment().subtract(29, 'days'), moment()],
     'This Month': [moment().startOf('month'), moment().endOf('month')],
     'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  }
});

5. Execute a callback function after you’ve got chosen a date range.

$('#myInput').daterangepicker({
  // options here
},function(start, end, label) {
  console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
});

6. Default plugin choices.

$('#myInput').daterangepicker({

  // 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 <input type="text" /> 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()
  },

})

7. Set begin/finish dates.

$('#myInput').data('daterangepicker').setStartDate('08/01/2021');
$('#myInput').data('daterangepicker').setEndDate('08/31/2021');

8. Available events.

$('#myInput').on('show.daterangepicker', function(ev, picker) {
  // do something
});

$('#myInput').on('hide.daterangepicker', function(ev, picker) {
  // do something
});

$('#myInput').on('showCalendar.daterangepicker', function(ev, picker) {
  // do something
});

$('#myInput').on('hideCalendar.daterangepicker', function(ev, picker) {
  // do something
});

$('#myInput').on('apply.daterangepicker', function(ev, picker) {
  // do something
});

$('#myInput').on('cancel.daterangepicker', function(ev, picker) {
  // do something
});

Feature-rich jQuery Datetime Range Picker, Date Range Picker Plugin/Github, datepicker date range example


See Demo And Download

Official Website(dangrossman): Click Here

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

Be First to Comment

    Leave a Reply

    Your email address will not be published.