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()
);