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

293
Vistas
How to get current week date in javascript?

I have a problem getting the current week date in javascript. I wrote the following code to get the current week's date, It was working well but it seems it sometimes returns the next week's date. I could not understand where is the problem. any help would be appreciated.

This is my code:

let curr = new Date();
let week = [];

for (let i = 1; i <= 7; i++) {
  let first = curr.getDate() - curr.getDay() + i;
  let day = new Date(curr.setDate(first)).toISOString().slice(0, 10);
  week.push(day);

};
console.log(week)

The output:

['2022-06-13', '2022-06-14', '2022-06-15', '2022-06-16', '2022-06-17', '2022-06-18', '2022-06-19']

but today's date is 6/12/2022, but the above date started from 13/06/2022

Source: here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

When you do +i you are adding numbers 1 to 7 to your first variable. Now since the week starts from Sunday=0, you do not need to add an offset.

Just iterate loop from 0 to 6:

let curr = new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000));
let week = [];

for (let i = 0; i < 7; i++) {
  let first = curr.getDate()  - curr.getDay() + i;
  let day = new Date(curr.setDate(first)).toISOString().slice(0, 10);
  week.push(day);

};
console.log(week)

[Edit] : If you want to get from 7 days ago, you can change how you initialize your curr variable.

From this :

let curr = new Date();

to this:

let curr = new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000));

I use the getTime() method to get the epoch of today's date and subtract exactly the milliseconds passed in 7 days (or 1 week).

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand your question correctly, you must take into consideration when current day is sunday to get current week first day (monday).

let curr = new Date();
let week = [];

for (let i = 1; i <= 7; i++) {
  let first = curr.getDate() - curr.getDay() + i;
  
  if (curr.getDay() === 0){
    first = curr.getDate() - 6;
  }
  let day = new     Date(curr.setDate(first)).toISOString().slice(0, 10);
  week.push(day);

};
console.log(week)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Start the for loop from 0

Like so

let curr = new Date();
let week = [];

for (let i = 0; i < 7; i++) {
  let first = curr.getDate() - curr.getDay() + I;
  let day = new Date(curr.setDate(first)).toISOString().slice(0, 10);
  week.push(day);

};
console.log(week)

Remember, if you start from 1 and add 1 to the current date it will return tomorrow, and if you add 0 to today it retustns this date.

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