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

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

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 Relatório

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 Relatório

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 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