How to format a date of type Date () and add a number of days depending on a format entered?

3

Good afternoon everyone,

I would like your valuable help to be able to do a function that adds me the days to any date entered by the user, I have been looking for a good time on the web but the only thing that appears is to add days to the current date,

By clicking on the input datepicker, add me 122 or 125 days depending on the length of the format. The problem I have is that when I want to capture the date that the user entered I appear Tue Mar 24 2015 18:00:00 GMT-0600 (Central America Standard Time) so when I want to format the date it tells me that the function getDate (_) it is not valid

My code at the moment is this:

function sumDias(fecha, numDias) {
  fecha.setDate(fecha.getDate() + numDias);
  return fecha;
};

function zero(n) {
  return (n > 9 ? '' : '0') + n;
};

$("#date1_record").on("change", function() {
  var id_format = $("#id_format_record").val();
  var id_format_array = id_format.split('-');
  var id_format = id_format_array[1];
  var numDias = parseInt(id_format);
  alert(numDias);
  if (isNaN(numDias)) {
    $('#date2_record').text('');
    return;
  }
  var fecha = document.getElementById('date1_record').value;
  var fechaActual = new Date(fecha);
  //var fechaActual = fechaActual0.getFullYear() + "-" + zero(fechaActual0.getMonth()+1) + "-" + zero(fechaActual0.getDate());
  alert(fechaActual);
  var fechaCalculada = sumDias(fechaActual, numDias);
  $('#date2_record').text(fechaCalculada.toString());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2">
  <label for="date1_record" title="Fecha Ingreso"><h6><strong>*Fecha Inicio</strong></h6></label>
  <input id="date1_record" type="text" class="form-control date" style="margin-bottom: 10px">
</div>
<div class="col-md-2">
  <label for="date2_record" title="Fecha Ingreso"><h6><strong>*Fecha Final</strong></h6></label>
  <input id="date2_record" type="text" class="form-control date" style="margin-bottom: 10px" disabled>
</div>

Your help please ... !!

    
asked by Khriz01 14.03.2018 в 23:07
source

2 answers

2

I added the id_format_record element to the OP code as I suggested in a comment. At least in Stack Snippet with this it works correctly.

  • The number of days must be entered n-n, example 2-122
  • The start date must be entered in the form yyyy-mm-dd example 2018-03-24

function sumDias(fecha, numDias) {
  fecha.setDate(fecha.getDate() + numDias);
  return fecha;
};

function zero(n) {
  return (n > 9 ? '' : '0') + n;
};

$("#date1_record").on("change", function() {
  var id_format = $("#id_format_record").val();
  var id_format_array = id_format.split('-');
  var id_format = id_format_array[1];
  var numDias = parseInt(id_format);
  alert(numDias);
  if (isNaN(numDias)) {
    $('#date2_record').text('');
    return;
  }
  var fecha = document.getElementById('date1_record').value;
  var fechaActual = new Date(fecha);
  //var fechaActual = fechaActual0.getFullYear() + "-" + zero(fechaActual0.getMonth()+1) + "-" + zero(fechaActual0.getDate());
  alert(fechaActual);
  var fechaCalculada = sumDias(fechaActual, numDias);
  $('#date2_record').text(fechaCalculada.toString());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2">
  <label for="id_format_record" title="Fecha Ingreso"><h6><strong>*Días</strong></h6></label>
  <input id="id_format_record" type="text" class="form-control date" style="margin-bottom: 10px">
</div>
<div class="col-md-2">
  <label for="date1_record" title="Fecha Ingreso"><h6><strong>*Fecha Inicio</strong></h6></label>
  <input id="date1_record" type="text" class="form-control date" style="margin-bottom: 10px">
</div>
<div class="col-md-2">
  <label for="date2_record" title="Fecha Ingreso"><h6><strong>*Fecha Final</strong></h6></label>
  <input id="date2_record" type="text" class="form-control date" style="margin-bottom: 10px" disabled>
</div>

Initial response

The problem is that your code expects the number of days in format If getDate is not valid, then the date variable does not refer to an object of type date.

Use new Date() to convert the string to date. Example:

var cadena = 'Tue Mar 24 2015 18:00:00 GMT-0600 (Central America Standard Time)';
var fecha = new Date(cadena);
var numDias = 5;

console.info(sumDias(fecha,numDias));

function sumDias(fecha, numDias) {

  fecha.setDate(fecha.getDate() + numDias);
  return fecha;
};
    
answered by 16.03.2018 / 16:48
source
0

I just resolved Ruben, thank you very much for your advice and suggestions, modify the code and leave it this way

    function sumDias(fecha, numDias){
     fecha.setDate(fecha.getDate() + numDias);
     return fecha;
    };
    function zero(n) {
     return (n>9 ? '' : '0') + n;
   };
   $(document).ready(function() {
   $("#date1_record").on("change", function(){
    var id_format = $("#id_format_record").val();
    var id_format_array = id_format.split('-');
    var id_format = id_format_array[1];
    var numDias = parseInt(id_format);
    //alert(numDias);
     if (isNaN(numDias)) {
      $('#date2_record').text('');
      return;
     }
     var fecha = $("#date1_record").val();
     var fechaActual = new Date(fecha);
     //var fechaActual = fechaActual0.getFullYear() + "-" + 
     zero(fechaActual0.getMonth()+1) + "-" + zero(fechaActual0.getDate());
     //alert(fechaActual);
     var fechaCalculada = sumDias(fechaActual, numDias);
     var fechaCalculada2 = new Date(fechaCalculada);
     var fechaCalculada3 = fechaCalculada2.getFullYear() + "-" + 
     zero(fechaCalculada2.getMonth()+1) + "-" + 
     zero(fechaCalculada2.getDate());
     $('#date2_record').val(fechaCalculada3);
     });
    
answered by 16.03.2018 в 21:40