Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How to get the Monday of the current week (or previous week in some cases) regardless of the week starts on Sunday or Monday

I have let today = new Date() object. I need to get the Monday of the current week regardless if the week starts on Sunday or Monday. If the week starts on a Sunday and the today is Sunday I need to get the Monday on the previous week. Sunday should always be the last day. Any suggestions on how to solve this?

Now I use this, which at least works fine when the week starts on Monday.

// 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 Respuestas
Responde la pregunta

0

Your code DOES get the previous Monday on a Sunday

What does "If the week starts on a Sunday" mean to your?

In JS Sunday is the 0th or first day

// 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda