Mohammad Shraim
Mohammad Shraim

Reputation: 1313

Bootstrap 3 Datepicker v4 call set date with moment or date

Iam using http://eonasdan.github.io/bootstrap-datetimepicker/

i have a datetime picker called dtpFrom

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

   <script> 
    $('#dtpFrom').datetimepicker({
        useCurrent: true,
        format: 'DD/MM/YYYY',
        showTodayButton: true,
        showClear: true,
        showClose: true,
        //autoclose: true,
        maxDate: new Date(),
        toolbarPlacement:'top'
    });
   </script>

i want to set its value on run time on other controls event.. i tried this and many other code but doesnt work!!

   $('#dtpFrom').data("DateTimePicker").date(moment(new Date('DD/MM/YYYY'), 'DD/MM/YYYY'));
   $('#dtpFrom').data("DateTimePicker").date(moment(new Date()));
   $('#dtpFrom').data("DateTimePicker").viewDate(new Date());

any suggestion.. thanks

Upvotes: 0

Views: 6690

Answers (2)

Joboman555
Joboman555

Reputation: 83

Mohammad's answer did not work for me. Turns out the syntax was slightly different, possibly due to a different version of the datepicker.

The following worked for me:

('#dtpFrom').data("DateTimePicker").setDate(new Date())

Upvotes: 0

Mohammad Shraim
Mohammad Shraim

Reputation: 1313

i found the solution if someone face the same problem

 $('#dtpFrom').data("DateTimePicker").date(moment(new Date()).format('DD/MM/YYYY'));

NOTE: the format should match the definition format option of datetimepicker.

Upvotes: 2

Related Questions