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

230
Views
Obtener fecha anterior en JavaScript

Estoy tratando de obtener la fecha anterior a una fecha determinada en JavaScript. Esto es lo que se me ocurrió.

 function getPreviousDay(year, month, day) { const date = new Date(year, month - 1, day); // the month is 0-indexed date.setDate(date.getDate() - 1); console.log(`${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`); } getPreviousDay(2016, 9, 30); // 2016-8-29, Expected output: 2016-9-29 getPreviousDay(2016, 10, 1); // 2016-8-30, Expected output: 2016-9-30

¿Por qué no funciona como se esperaba? ¡Gracias de antemano!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Haces month - 1 , al inicializar la fecha, pero debería ser un mes.

about 4 years ago · Juan Pablo Isaza Report

0

Como dijiste, los meses, en el objeto de fecha js, están indexados en 0.

Así que haces muy bien en restarle uno al mes, al crear el objeto de fecha.

Su problema es que no vuelve a agregar ese mes cuando muestra el objeto de fecha:

 console.log(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`);
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!