0

I want to pass the value from datetimepicker1 to datetimepicker2 and add 12hours on datetimepicker2.

Passing value from datetimepicker to datetimepicker2 is working on my code.
But how can I add 12 hours on datetimepicker2?

<div class="row">
  <div class="col-md-12">
    <form id="form1" method="post">
      <div class="form-group">
        <div class='input-group date' id='datetimepicker1'>
          <input type="text" id="r_from"  name="r_from" class="form-control" >
          <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
      <div class="form-group">
        <div class='input-group date' id='datetimepicker2'>
          <input type="text" id="r_until"  name="r_until" class="form-control" ><br>
          <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
      <input type="submit" name="check_cottage" value="Check Availability">
    </form>
  </div>

<script type="text/javascript">
$(function () {
  $('#datetimepicker1').datetimepicker();
  $('#datetimepicker2').datetimepicker({
    useCurrent: false //Important! See issue #1075
  });

  $("#datetimepicker1").on("dp.change", function (e) {
    $('#datetimepicker2').data("DateTimePicker").minDate(e.date);
  });

  $("#datetimepicker2").on("dp.change", function (e) {
    $('#datetimepicker1').data("DateTimePicker").maxDate(e.date);
  });
});
</script>

<script type="text/javascript">
var form = document.getElementById('form1');
form.elements.r_from.onblur = function () {
  var form = this.form;
  form.elements.r_until.value = form.elements.r_from.value;
};
</script>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64

1 Answers1

0

It finally wasn't an easy one.
I quickly fixed an error in console about Tether lib missing...

Then I played a lot with Moment object formatting.
This worked great, since the documentation is real good.
The thing was to find what BootStrap DateTimePicker was expecting as a date format...

It took time to question things I assumed as correct from start.
like this option:

useCurrent:false //Important! See issue #1075

And to set the date... Not only the maxDate, but the date of the second DP...
I found it by just guessing... Since the documentation is &$?%!

$('#datetimepicker2').data("DateTimePicker").date( [the new date + 12h] );

And it worked!
See your updated Fiddle

$(function () {
  $('#datetimepicker1').datetimepicker();
  $('#datetimepicker2').datetimepicker({
    useCurrent:"year"
  });

  $("#datetimepicker1").on("dp.hide", function (e) {
    console.log("From DP: "+e.date);

    var momentDate = moment(e.date);
    console.log("DP1 Moment hour: "+momentDate.hour());

    var improvedDate = moment(e.date).add(12,"hour");
    console.log("DP2 Moment date: "+improvedDate.hour());

    var improvedDateForDP = improvedDate.format("MM/DD/YYYY hh:mm:ss A");
    console.log("DP2 Moment hour: "+improvedDateForDP);

    $('#datetimepicker2').data("DateTimePicker").minDate(improvedDate);
    $('#datetimepicker2').data("DateTimePicker").date(improvedDate);
  });

  $("#datetimepicker2").on("dp.change", function (e) {
    $('#datetimepicker1').data("DateTimePicker").maxDate(e.date);
  });

});

I left all console.logs...

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64