Sum of javascript date

Asked

Viewed 180 times

0

In the function add and generated through a is the amount of plots that will have, accurate that manages a maturity date each time there is plot.

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++;
  • PHP or Javascript?

  • "I need you to manage a due date", and what exactly do you want to do? Add what to what?

  • @Giovanninunes In Javascript.

  • @Guilhermeconstamilam I will add a date through an input Instalments and the number of plots through Qtdparcelas for each installment I want a different month, ie Qtdparcelas is the month amount, need to add in Installments due for each installment.

2 answers

1

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

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: 23/04/2018, 23/05/2018, 22/06/2018, 22/07/2018, 21/08/2018 and 20/09/2018; But since it is not every month that have exactly 30 days you will have a due date offset.

The other alternative is to consider calculating the date using the month instead of the day, something like that (I changed date and number of plots 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 expiration dates 30/04/2018, 30/05/2018, 30/06/2018, 30/07/2018, 30/08/2018, 30/09/2018, 30/10/2018, 30/11/2018, 30/12/2018, 30/01/2019, 02/03/2019 and 30/03/2019; In this case with dates closer to what people normally expect.

  • On my return it appeared Mon Aug 20 2018 00:00:00 GMT-0300 (Hora oficial do Brasil) how to place with normal date? dd/mm/yyyy

  • You are using the method .toLocaleDateString() to convert the object into a string? Otherwise it will always appear like this in the default date format.

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);

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.