Add days to date of input date

Posted on

Question :

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:

$('#txtSolicitacao').on('blur', function(){
    data = $('#txtSolicitacao').val() + 7 ; 

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


Answer :

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;

<script src=""></script><inputtype="date" id="txtSolicitacao">
<input type="date" id="txtTermino">


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");
    Data Inicio: 
    <input id="dataInicio" type="date" value="2016-02-26" />
    Data Final: 
    <input id="dataFinal" type="date" />


Leave a Reply

Your email address will not be published. Required fields are marked *