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

177
Views
Cómo obtener el lunes de la semana actual (o de la semana anterior en algunos casos) independientemente de que la semana comience el domingo o el lunes

He let today = new Date() . Necesito obtener el lunes de la semana actual, independientemente de si la semana comienza el domingo o el lunes. Si la semana comienza en domingo y today es domingo, necesito obtener el lunes de la semana anterior. El domingo siempre debe ser el último día. ¿Alguna sugerencia sobre cómo resolver esto?

Ahora uso esto, que al menos funciona bien cuando la semana comienza el lunes.

 // Get the monday date of current week function getMondayOfCurrentWeek(d) { const date = new Date(d); const day = date.getDay(); // Sunday - Saturday : 0 - 6 // Day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return new Date(date.setDate(diff)); } console.log( getMondayOfCurrentWeek(new Date(2022,3,3)).toDateString() );

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

0

Su código SÍ recibe el lunes anterior en domingo

¿Qué significa para ti "Si la semana comienza en domingo"?

En JS el domingo es el día 0 o primero

 // Get the monday date of current week function getMondayOfCurrentWeek(d) { const date = new Date(d); const day = date.getDay(); // Sunday - Saturday : 0 - 6 // Day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return date.setDate(diff), date; } console.log(getMondayOfCurrentWeek(new Date()).toDateString() ) console.log(getMondayOfCurrentWeek(new Date(2022,3,3,15,0,0,0)).toDateString() ); // Sunday 3rd of April 2022

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!