Search code examples
javascripteonasdan-datetimepicker

Unable to select days and hours when minDate and maxDate are on the same days with Eonasdan datetimepicker


I got an odd behaviour when using bootstrap-datetimepicker v. 4.17.47. Here is my picker configuration:

format: 'YYYY-MM-DD HH:mm',
showTodayButton: true,
useCurrent: false,
showClear: true,
minDate: moment('{{ ts_beg }}'),
maxDate: moment('{{ ts_end }}')

When setting minDate and maxDate to the same day but with different hours, let's say 2018-2-1 00:00 and 2018-2-1 02:00, I am unable to choose both date and time: peek 2018-02-05 09-33

Does anyone has a workaround to solve this issue ?


Solution

  • The problem, if not wrong, is because of the implementation of maxDate you can override this behavior using the functions, this way you will be able to select the date

    Here a live example:

    const format = 'YYYY-MM-DD HH:mm';
    const MaxDate = moment('2018-02-01 02:00', format);
    const MinDate = moment('2018-02-01 00:00', format);
    $('#myDatepicker').datetimepicker({
      format: format,
      showTodayButton: true,
      useCurrent: false,
      showClear: true,
      minDate: MinDate,
    });
    
    $('#myDatepicker').data("DateTimePicker").maxDate(MaxDate);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/25c11d79/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>
    
    
    <br/>
    <!--Space for the fiddle-->
    <div class="container">
      <div class="row">
        <div class='col-sm-6'>
          <div class="form-group">
            <div class='input-group date' id='myDatepicker'>
              <input type='text' class="form-control" />
              <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>