Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda