uxsolutions / bootstrap-datepicker

A datepicker for twitter bootstrap (@twbs)
Apache License 2.0
12.67k stars 6.07k forks source link

Unable to restrict dates in daterange... #2350

Open jeremyswright opened 6 years ago

jeremyswright commented 6 years ago

I'm trying to have a start and end date that work with each other (i.e. start date cannot be after end date and vice versa).

`

      <span class="input-group-addon">to</span>
      <div class="input-group date">
        <input type="text" class="input-sm form-control" id="inputEndDate" name="end" />
        <span class="input-group-addon"><i class="calendar"></i></span>
      </div>
    </div>`

...and...

` $('.input-group.date, .input-daterange').datepicker({ keyboardNavigation: false, showOnFocus: false, autoclose: true });

  $('#inputStartDate').on('change', function () {
    console.log('change triggered');

// $("#inputDate").attr('data-date-start-date', '01-02-2018');

    $("#inputEndDate").datepicker("clearDates");
    $("#inputEndDate").datepicker("setStartDate", new Date($("#inputStartDate").val()));
  })

` I can't get the setStartDate or setEndDate to work. They just don't seem to do anything. I've tried setting data-date-start-date but had no success down that route. It works fine when I set startDate on instantiation.

Datepicker version used

ex. 1.7.1.

I incorporated the code from this fiddle (https://jsfiddle.net/qwc0dwLa/1/) and managed to get two datepickers to interact. The only difference I can see is that mine is using the daterange.

Any advice greatly appreciated.

yugaego commented 6 years ago
jeremyswright commented 6 years ago

@yugenekr Thanks, but how do I set a start or end date? The setStartDate doesn't seem to do anything.

jeremyswright commented 6 years ago

So I managed to get setStartDate to do something...

The issue was that I was passing a moment rather than a Date..

var from = $(this).val().split("-"); var f = new Date(from[2], from[1] - 1, from[0]); $('#inputEndDate').datepicker('setStartDate', f);

However, calling setStartDate (or setEndDate) for that matter trashes all my options that I setup when I instanciated the datepickers. Given the amount of time I've spent on this and the pitiful progress I've made, no option now but to hack the code. :-(