Add days to date of input date

4

I have a input of type date, I would like to add to it 7 days and put that value in another input also of type date, I tried to do so:

<script>
$('#txtSolicitacao').on('blur', function(){
    data = $('#txtSolicitacao').val() + 7 ; 
    $('#txtTermino').val(data);
});
</script>

Note: the date is in the American year-month-day format.

    
asked by anonymous 04.03.2016 / 18:12

2 answers

2

I suppose this is what you are looking for.

    $('#txtSolicitacao')[0].valueAsDate = new Date();

    $('#txtSolicitacao').change(function() {
      var date = this.valueAsDate;
      date.setDate(date.getDate() + 7);
      $('#txtTermino')[0].valueAsDate = date;
    });

    $('#txtSolicitacao').change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="date" id="txtSolicitacao">
<br>
<input type="date" id="txtTermino">
    
04.03.2016 / 18:35
1

Considering that the input [type = 'date'] stores the date in ISO format, you can do the following:

Unfortunately the code below will only be friendly to Chromium-based browsers, but the logic to add 7 days remains the same.

var dataInicio = document.getElementById("dataInicio");
var dataFinal = document.getElementById("dataFinal");

dataInicio.addEventListener("focusout", function (event) {
  var offset = new Date().getTimezoneOffset();
  var data = new Date(dataInicio.value);
  data.setMinutes(data.getMinutes() + offset);
  data.setDate(data.getDate() + 7);

  dataFinal.value = data.toISOString().substring(0, 10);
})

var event = new Event("focusout");
dataInicio.dispatchEvent(event);
<div>
  <label>
    Data Inicio: 
    <input id="dataInicio" type="date" value="2016-02-26" />
  </label>
</div>
<div>
  <label>
    Data Final: 
    <input id="dataFinal" type="date" />
  </label>
</div>
    
04.03.2016 / 18:34