Date sum javascript

0

In the add-and-generate-by-one function, the amount of installments that you have will require you to generate a due date each time you have a parcel.

function adicionar(){
      ParcelaVencimento=$("#ParcelaVencimento").val();   
      ParcelaValor=$("#ParcelaValor").val();
      QtdParcela=$("#QtdParcela").val();

      for ($i = 0; $i < QtdParcela; $i++){

            subtotal[cont]=(ParcelaValor*1);
            total = total + subtotal[cont];
            var linha = '<tr class="selected" id="linha'+cont+'">    <td> <button type="button" class="btn btn-warning" onclick="apagar('+cont+');"> X </button></td>      <td> <input type="hidden" name="cont[]" value="'+cont+'">'+cont+'</td>  <td> <input type="date" name="ParcelaVencimento[]" value="'+ParcelaVencimento+'"></td> <td> <input type="number" name="ParcelaValor[]" value="'+ParcelaValor+'"></td> <td> <input type="number" name="QtdParcela[]" value="'+QtdParcela+'"></td> </tr>'

            cont++;
    
asked by anonymous 23.04.2018 / 01:25

2 answers

1

There are two approaches to this problem, in the first you consider that there is an exact interval of 30 days to the due date of each plot:

let dia = 23;
let mes = 4-1; // janeiro = 0
let ano = 2018;
let parcelas = 6;

for (let i=0; i<parcelas; i++){
    dataVencimento = new Date(ano, mes, dia + 30*i);
    dataComoString = dataVencimento.toLocaleDateString();
    console.log(dataComoString);
}

What will generate the due dates: 04/23/2018, 05/23/2018, 06/22/2018, 07/22/2018, 08/21/2018 and 09/20/2018; But since it is not every month that you have exactly 30 days you will have an expiration date.

The other alternative is to consider the date calculation using the month instead of the day, something like that (I changed date and number of parcels on purpose):

let dia = 30;
let mes = 4-1; // janeiro = 0
let ano = 2018;   
let parcelas = 12;

for (let i=0; i<parcelas; i++){
    dataVencimento = new Date(ano, mes + i, dia);
    dataComoString = dataVencimento.toLocaleDateString();
    console.log(dataComoString);
}

That will produce the due dates 04/30/2018, 05/30/2018, 06/30/2018, 07/30/2018, 08/30/2018, 09/30/2018, 10/30/2018 , 11/30/2018, 12/30/2018, 1/30/2019, 03/02/2019 and 03/30/2019; In this case with dates closer to what people normally expect.

    
24.04.2018 / 01:18
0

I tried to adapt the variables day / month / year and I have the following problem, the day decreases the value of i, what went wrong?

  for (let i=0; i<QtdParcela; i++){
        var Vencimento = ParcelaVencimento.split("/");
        var DataVencimento = Vencimento[2]+"-"+Vencimento[1]+"-"+Vencimento[0];
        var myDate = new Date(DataVencimento);
        var ano = myDate.getFullYear();
        var dia = myDate.getDate(); if(dia<10){dia='0'+dia};
        var mes = (myDate.getMonth()+1); if(mes<10){mes='0'+mes} 

        ParcelaVencimento = new Date(ano, mes + i, dia);
        ParcelaVencimento =  (dia+"/"+mes+"/"+ano);

    
25.04.2018 / 02:40