How can one return the next date of a given weekday (it could be either a number 0-6 or names Sunday-Saturday).
Example, if today, on Friday 16-Oct-2009 I passed in:
function getSundayOfCurrentWeek() { const today = new Date(); const first = today. getDate() - today. getDay() + 1; const last = first + 6; const sunday = new Date(today. setDate(last)); return sunday; } // (today is Mon Jan 17 2022) // 👇️ Sun Jan 23 2022 console.
JavaScript - Date getDay() Method Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
You can also use following lines of code to get first and last date of the week: var curr = new Date; var firstday = new Date(curr. setDate(curr. getDate() - curr.
Just adding 7 doesn't solve the problem.
The below function will give you the next day of the week.
function nextDay(x){ var now = new Date(); now.setDate(now.getDate() + (x+(7-now.getDay())) % 7); return now; }
Here's a slightly modified version to Tim's answer to address the specific question-- pass in a date d, and, and a desired day of week (dow 0-6), return the date
function nextDay(d, dow){ d.setDate(d.getDate() + (dow+(7-d.getDay())) % 7); return d; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With