Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Agregar años a la matriz de fechas

si al final del período la persona tiene más de 18 años, quiero que childEndDate sea la fecha de la persona en la que cumplirá 18 años. en mi declaración else if, estoy usando la biblioteca date-fns para agregar 18 años a las fechas que tengo en mi matriz this.childBirthDate . pero la salida devuelta es incorrecta: ["1988-01-01T00:00:02.012Z", "1988-01-01T00:00:02.010Z", "2031-11-08T13:24:43.704Z"] la salida que quiero devolver es: ['2030-02-16T20:00:00.000Z', '2028-05-19T20:00:00.000Z', 2031-11-08T13:24:43.704Z]

aquí está mi stackblitz

 this.childBirthDate = [ '2012-02-16T20:00:00.000Z', '2010-05-19T20:00:00.000Z', '2016-05-19T20:00:00.000Z', ]; //enddate const endYear = date.getFullYear() + 10; date.setFullYear(endYear); this.endDate = date.toISOString(); this.childBirthDate.forEach((element) => { const birthYear = element.substring(0, 4); this.childbirthYear.push(+birthYear); }); const periodEndYear = +this.endDate.substring(0, 4); // calculate child endDate this.childbirthYear.forEach(element => { if (periodEndYear - element < 18) { this.childEndDate = this.endDate; } else if (periodEndYear - element >= 18) { this.childEndDate = addYears(new Date(element), 18).toISOString(); } this.final.push(this.childEndDate); }); console.log(this.final) this.personalInfo = { personalInfoId: 0, underageChildInfo: this.data.underageChildInfo?.map((i, index) => ({ firstName: i.name, endDate: this.final[index], })), };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que su lógica para obtener la fecha de finalización es reproducir lo que desea. Sin embargo, creo que su código puede ser difícil de seguir debido a todas las variables adicionales.

Si elimina los bucles foreach a favor de algunos operadores de map , su código registrará las fechas que desea obtener.

p.ej

 // in ngOnInit this.finalDates = this.childBirthDate.map(date => { return this.calculateDate(date); }); // outputs ["2030-02-16T20:00:00.000Z", "2028-05-19T20:00:00.000Z", "2031-11-08T14:28:01.761Z"] console.log(this.finalDates);

 // custom method on the class calculateDate(date: string): string { const periodEndYear = +this.endDate.substring(0, 4); const year = parseInt(date.substring(0, 4), 10); // get a number from string if (periodEndYear - year < 18) { return this.endDate; } else if (periodEndYear - year >= 18) { return addYears(new Date(date), 18).toISOString(); } return date; }

aquí hay un tenedor de tu blitz

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!