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

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

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