I'm trying to get the date of Monday of the upcoming week, so for example, if I run the script now I will get 2021-09-13.
I tried solving this myself, and wrote this:
export function getMondayDate() {
const d = new Date();
const DAYS_IN_WEEK = 7;
var today = d.getDay()
const daysToMonday = (DAYS_IN_WEEK - today) + 1;
const Monday = d.getDate() + d.getDay() + daysToMonday;
return new Date(d.setDate(Monday));
}
I thought about getting the current day, so for example getDay()
will return 3
.
So, when subtracting DAYS_IN_WEEK
from today
, will result in the number 4
that would get Sunday and than to add 1
to get Monday.
So, from Today, it will be 5
days to get to Monday.
But for some reason I get the wrong date, I can't really see what's wrong here.