How to get the current weekday date in JavaScript

I have a function that takes the desired day number and ultimately I want it to be able to return the date of that day in the current week, given the day number it takes. This is my code.

function getDateForDayInWeek(day) {
    const today = new Date();
    const currentDay = (today.getDay() + 1) % 7;
    const adjustedDay = currentDay === 0 ? 6 : currentDay - 1;
    const daysToAdjust = (day - adjustedDay + 7) % 7;

    today.setDate(today.getDate() + daysToAdjust);

    // Formatting the date to YYYY-MM-DDTHH:mm:ss
    return today.toISOString().slice(0, 19);
}


console.log(getDateForDayInWeek(6)); //  2024-12-21 ,Saturday
console.log(getDateForDayInWeek(0)); //  2024-12-22 ,Sunday
console.log(getDateForDayInWeek(5)); //  2024-12-27 ,Friday