Increment Ionic data 3, Angular, Typescript

Asked

Viewed 473 times

0

I would like to know how to increase the expiration date of a purchase. Example: the person made a purchase, and put the due date for the day 30 March, then the date of the second installment would be 29 April. Follow function and HTML code.

<ion-card *ngFor="let p of parcelasCobranca; let i = index">
  Parcela
  <ion-item>
    <ion-label stacked>Valor: {{ valorParcela }}</ion-label>
  </ion-item>
  <ion-item>
    <ion-label stacked>Data de vencimento *</ion-label>
    <ion-input type="date"  formControlName="due_date" name="due_date" id="due_date"></ion-input>
  </ion-item>
  <ion-item *ngIf="due_date.hasError('required') && due_date.touched">
    <p>Digite a data *</p>
  </ion-item>
</ion-card>
parcelas() {
  this.parcelasCobranca = [];
  this.valorParcela = this.amount / this.quantidadeParcelas;
  for (let i = 0; i < this.quantidadeParcelas; i++) {
     this.parcelasCobranca.push({
       due_date:this.due_date.value,
       value: this.valorParcela,
     });
   }

   console.log(this.parcelasCobranca)
 }

Update 1 - follows the other code I made, the due date is going back a few days.

parcelas() {

  this.parcelasCobranca = [];
  this.valorParcela = this.amount / this.quantidadeParcelas;
  let someDate = new Date(this.due_date.value);

  for (let i = 0; i < this.quantidadeParcelas; i++) {

    someDate.setDate(someDate.getDate() + 30);


    let dd = someDate.getDate();
    let mm = someDate.getMonth();
    let y = someDate.getFullYear();

    let someFormattedDate = dd + '-' + mm + '-' + y;

    this.parcelasCobranca.push({

      due_date: someFormattedDate,
      value: this.valorParcela,

    });
  }

  console.log(this.parcelasCobranca)
}
<ion-grid [hidden]="!value">
  <ion-card *ngFor="let p of parcelasCobranca; let i = index">
    Parcela
    <ion-item>
      <ion-label stacked>Valor: {{ valorParcela }}</ion-label>
    </ion-item>
    <ion-item>
      <ion-label stacked>Data de vencimento *</ion-label>
      <ion-input type="date"  formControlName="due_date" name="due_date" id="due_date"></ion-input>
    </ion-item>
    <ion-item *ngIf="due_date.hasError('required') && due_date.touched">
      <p>Digite a data *</p>
    </ion-item>
  </ion-card>
</ion-grid>

1 answer

2


I recommend using the library Moment js., in his method parcelas() create a variable:

let data = moment(this.due_date.value);

In the for leave it so:

parcelasCobranca.push({
  value: this.valorParcela,
  due_date: moment(data).format('L'),
});
// A cada loop adiciona 30 dias em data
data = moment(data).add(30, 'days');

Working example:

let amount = 2000;
let parcelasCobranca;

let quantidadeParcelas = document.querySelector('#quantidadeParcelas');
let due_date = document.querySelector('#due_date');

function parcelas() {
  parcelasCobranca = [];
  let parcelas = parseInt(quantidadeParcelas.value);
  let valorParcela = amount / parcelas;
  let data = moment(due_date.value);
  for (let i = 0; i < parcelas; i++) {
    parcelasCobranca.push({
      value: valorParcela,
      due_date: moment(data).format('L')
    });
    // A cada loop adiciona 30 dias em data
    data = moment(data).add(30, 'days');
  }
  console.log(parcelasCobranca);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/locale/pt-br.js"></script>
Parcelas: <input type="number" id="quantidadeParcelas" value="1" min="1" />
<input type="date" id="due_date" />
<button id="gerarParcelas" onclick="parcelas()">Parcelas</button>

  • Man, I mean, for the help, that’s what I wanted. But I wanted to ask a question, I had redone the code here, but when I put a date it’s going back a few days. Example, I put the date of the first expiration for the day 30 March, but when I order print on the console the expiration date appears 28 March. I’ll put this code in the post.

  • You’ve done as you are here ?

  • It’s a little different

  • Use the library Moment js. will facilitate a lot in working with dates, just see both lines of code you wrote and look at that of the answer.

Browser other questions tagged

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