I need to calculate the date of the last Monday that before the latest weekend. Having used what I believe to be the most common Stack Overflow suggestions I have the following code:
const getDayOfTheWeek = () => {
let date = new Date();
let clonedDate = new Date(date.getTime());
console.log(clonedDate);
const dow = clonedDate.getDay();
console.log(dow);
const offset = dow+6;
console.log(offset);
const newDate = new Date(clonedDate.setDate(clonedDate.getDate() - offset));
console.log(newDate);
const newNewDate = new Date(newDate.getTime());
console.log(newNewDate);
const day = newNewDate.getDate();
const month = newNewDate.getMonth();
const year = newNewDate.getYear();
console.log('the year is ',year, 'the month is ', month);
}
getDayOfTheWeek();
It returns the year as 118 and the month as 5 which are ... not that Monday I need. newNewDate, on the other hand is the last Monday. I was wondering what causes it. I am aware that there are too many reassignments that are not needed. Please help.