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
To get the current weekday’s date in JavaScript, you can use the Date object and manipulate it to get the date of a specific day in the current week based on a given day number.
Here’s the breakdown of the function:
function getDateForDayInWeek(day) {
const today = new Date(); // Get today's date
const currentDay = today.getDay(); // Get the current day of the week (0 for Sunday, 1 for Monday, etc.)
// Calculate how many days to adjust to reach the desired day in the current week
const daysToAdjust = (day - currentDay + 7) % 7;
today.setDate(today.getDate() + daysToAdjust); // Adjust the date to the desired day
// Format the date in the 'YYYY-MM-DDTHH:mm:ss' format
return today.toISOString().slice(0, 19);
}
console.log(getDateForDayInWeek(6)); // Output: '2024-12-21T00:00:00' (Saturday)
console.log(getDateForDayInWeek(0)); // Output: '2024-12-22T00:00:00' (Sunday)
console.log(getDateForDayInWeek(5)); // Output: '2024-12-27T00:00:00' (Friday)
Explanation:
- today.getDay() returns the current day as a number (0 for Sunday, 1 for Monday, etc.).
- The expression (today.getDay() + 1) % 7 adjusts the current day so that Monday is 0, Tuesday is 1, and so on (making it easier to compare).
adjustedDay ensures that Sunday is considered as 6 (instead of 0). - daysToAdjust calculates the number of days to add or subtract to reach the desired day number.
- The date is adjusted with today.setDate(today.getDate() + daysToAdjust).
toISOString() is used to format the date in a consistent format, and .slice(0, 19) removes the milliseconds part.
This code will return the date for the given day number in the current week.
Abd Elaziz El7or is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Try this:
function getDateForDayInWeek(targetDay) {
const today = new Date();
const currentDayOfWeek = today.getDay(); // Returns 0-6 (Sunday-Saturday)
const daysToAdjust = targetDay - currentDayOfWeek;
const resultDate = new Date(today);
resultDate.setDate(today.getDate() + daysToAdjust);
return resultDate.toISOString().slice(0, 10); // Returns only YYYY-MM-DD
}
The key difference is that it now correctly calculates the dates for the current week without unnecessary complexity in the day calculations.
1