Search code examples
htmljquery-uidatepickerbootstrap-4eonasdan-datetimepicker

Date time picker : Reset


I am using a package. Specifically ThIS.

Everything works fine, I clicks on the date picker, I selects year, Then month and then date, And the picker closes.

But when it closes and i click on it again, Only the last day calendar shows, Meaning it doesn't starts over again from year.

What i want is that, It should starts over again from year to month to day.

Here is the JsFiddle

Here is my code :

<div class='input-group date' id='datetimepicker9'>
    <input type='text' class="form-control" />
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>

Js :

$(function () {
    $('#datetimepicker9').datetimepicker({
        viewMode: 'years'
    });
});

Solution

  • You can reset the viewMode using dp.hide event and a setTimeout

    let $datePicker = $('#myDatepicker').datetimepicker({
      useCurrent: false,
      viewMode: 'years'
    });
    
    $datePicker.on('dp.hide', function(event) {
      setTimeout(function() {
        $datePicker.data('DateTimePicker').viewMode('years');
      }, 1);
    });
    <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>