Home>

the inputs themselves work correctly

and the output below them is not

if you select the current date -it will display 0

if another -there will be a minus

$(function() {
  let text_min= '';
  let text_max= '';
  function dateTravel(dFrom, dTo) {
    $(dFrom).datepicker({
      minDate: 0
      dateFormat: 'dd.mm.yy',
      onSelect: function() {
        let minDate= $(this).datepicker('getDate');
        setTimeout(function() {
          minDate.setDate(minDate.getDate() + 1);
          $(dTo).datepicker('option', 'minDate', minDate);
        }, 300);
        text_min= $(this).val();
        $('#text_min').text(text_min);
        diffDate();
      }
    }).datepicker('setDate', '0');
    $(dTo).datepicker({
      minDate: 1
      dateFormat: 'dd.mm.yy',
      onSelect: function() {
        text_max= $(this).val();
        $('#text_max').text(text_max);
        diffDate();
      }
    }).datepicker('setDate', '1');
  }
  dateTravel('#date_from', '#date_to');
  $('#text_min').text($('#date_from').val());
  $('#text_max').text($('#date_to').val());
  function diffDate() {
    let date1= new Date(convert_date_format($('#text_min').text()));
    let date2= new Date(convert_date_format($('#text_max').text()));
    let date_diff= new Date(date2 -date1);
    let diff= date_diff /1000 /60 /60 /24;
    $('#days').text(diff);
  }
  function convert_date_format(date_string) {
    let date= date_string.split('.', 3);
    return date[1] + '/' + date[0] + '/' + date[2];
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" /><input type="text" id="date_from"><input type="text" id="date_to"><div>term</div>

<span id="days">1</span> days, from <span id="text_min">13</span> by <span id="text_max">14</span>p>

  • Answer # 1

    In my opinion, you create a new date and find it according to the principle (date2-date1), but you need the number of days, and this is not a date, but a number.

    Read this, it should help

    welcome to This Club in Russian! Links to external resources are fine, but leave a description along with the link so that other users have an idea of ​​its content. Always quote the most relevant information that you have taken from the indicated source, in case it is not available or permanently closed.

    aleksandr barakin2022-02-14 10:01:57