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

292
Views
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 answers
Answer question

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 Report

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 Report

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