Multiple instances of daterange pickers on the same page

javascript jquery twitter-bootstrap bootstrap-daterangepicker

698 观看


70 作者的声誉

I have two Bootstrap date range pickers. When I change the date of one picker, the same date appears in the second date range picker.

How can I separate the two date range pickers, so they each operate independently?


First date picker:

<div id="reportrange" class=" form-control">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
<b class="caret "></b>&nbsp;<span></span> 

Second date picker:

<div id="usersdaterange" class=" form-control"> <i class="glyphicon 
glyphicon-calendar fa fa-calendar"></i>&nbsp;<b class="caret "></b>&nbsp;


span ').on('
  apply.daterangepicker ',function(ev1,picker1) {
  var username1 = $("#userfilter").val();
  var start1 = picker1.startDate.format('YYYY-MM-DD');
  var end1 = picker1.endDate.format('YYYY-MM-DD');

$('#reportrange span').on('apply.daterangepicker', function(ev, picker) {
  var username = $("#userfilter").val();
  var start = picker.startDate.format('YYYY-MM-DD');
  var end = picker.endDate.format('YYYY-MM-DD');
作者: moazzam 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


1072 作者的声誉

Bootstrap daterangepicker works with multiple instances out of the box.

$(function() {
  $('#dateRange1').daterangepicker({}, onSelect);
  $('#dateRange2').daterangepicker({opens:"left"}, onSelect);

function onSelect(start, end, label) {
    console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));



<script src=""></script>
<script src=""></script>
<script src=""></script>
<link href="" rel="stylesheet"/>
<link href="" rel="stylesheet"/>

<input id="dateRange1" class="form-control" type="text" name="daterange" value="01/01/2018 - 01/15/2018" />

<input id="dateRange2" class="form-control" type="text" name="daterange" value="01/01/2018 - 01/15/2018" />


If the two are linked then it must be because there is some code that is updating them together. Perhaps the most obvious possibility is that there is some function being called on apply.daterangepicker that is then updating the other datepicker with setStartDate or setEndDate etc.

(Unfortunately it's not clear what the original askers problem is from the code they have posted, but I thought I would answer this question anyway to clear up any potential confusion for anyone searching for this)

作者: ChrisM 发布者: 01.04.2019 05:29