39

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:

  • Friday, it would return today's date 16-Oct-2009
  • Saturday returns 17-Oct-2009
  • Thursday returns 22-Oct-2009
Ruslan
  • 9,927
  • 15
  • 55
  • 89
  • @Andy E Why didn't you down-vote this question? I am sick and tired of users in this community neglecting to down-vote just because they lose a point. I would've never came here if it was at -1! – Josh Stodola Oct 16 '09 at 16:32
  • 1
    @Josh Stodola: I didn't down-vote because I like to give the user a chance to fix the problem first. I've down-voted many questions/answers, 1 point is nothing. I asked him to rephrase the question and if he does then the down-vote would be unnecessary. – Andy E Oct 16 '09 at 16:35
  • 1
    You can always retract your vote after the fact. Thats what I do. – Josh Stodola Oct 16 '09 at 16:38
  • 15
    I personally prefer poorly phrased questions to uncivil users. – slikts Oct 16 '09 at 16:41
  • @Josh Stodola: Well, I don't think there's anything wrong with the way I do things. I came back to see if the question had been rephrased and it hasn't, so it gets my -1. Sure you can retract a vote but it's very easy to forget to do so. – Andy E Oct 17 '09 at 09:58
  • @Reinis I: My comments aren't intended to be uncivil, I haven't been offensive or hostile in any of them. I think we can all agree that properly formed questions are the basis of well written, detailed answers and the less we see of poorly phrased questions the less we see of uncivil comments. – Andy E Oct 17 '09 at 10:01
  • @Andy & @Josh: ok girls, calm down =) the q is re-phrased, although I believe that most important is to well define a problem, rather than the "chit-chat" around it =) @Reinis: Paldies tev, draugs =) – Ruslan Oct 19 '09 at 08:52
  • 9
    @Andy, @Josh The FAQ clearly states: "As long your question is ... of interest to at least one other programmer somewhere ... it is welcome here. No question is too trivial or too 'newbie'". This is the questioner's first question, give her/him some slack -- no one gets it perfect on the first try. – brianpeiris Oct 19 '09 at 09:00
  • 2
    Hey @AndyE , "@slikts", @"John Stodola", @"brianpeiris" . Hope you're well. It's been 7.5 years ) I finally got around to editing the question)) – Ruslan Apr 26 '17 at 16:03
  • 1
    Better late than never, downvote retracted! :-) – Andy E Apr 27 '17 at 09:53
  • 3
    This is one of the most unwelcoming sites for newcomers. I'd rather struggle for hours with a problem than post a question here. – E L Mar 16 '19 at 15:22

6 Answers6

49

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;
}
Tim
  • 2,369
  • 18
  • 16
  • 12
    For the curious, this is how this works: The code gets the current day of the month(1-31) and adds the difference between today's day of the week (0-6) and the desired day of the week (0-6), then uses mod to make sure the new value isn't more than 6. – Polyducks May 25 '16 at 11:14
21

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;
}
NoelHunter
  • 996
  • 9
  • 17
  • 2
    For those who need to get a day of the week after next, or the week after that: add a week argument `function nextWeekDay(d, dow, w)` and then add the week to the calculation and remove the modulus: `returnDate.setDate(d.getDate() + (dow+((w * 7)-d.getDay())));` – stackingjasoncooper Nov 08 '17 at 16:04
12

Here is another simple solution

//takes dayIndex from sunday(0) to saturday(6)
function nextDate(dayIndex) {
    var today = new Date();
    today.setDate(today.getDate() + (dayIndex - 1 - today.getDay() + 7) % 7 + 1);
    return today;
}
document.write("Next Sunday is: "+nextDate(0).toLocaleString()+"<br/>");
document.write("Next Thursday is: "+nextDate(4).toLocaleString()+"<br/>");
document.write("Next Saturday is: "+nextDate(6).toLocaleString());
Hari Das
  • 10,145
  • 7
  • 62
  • 59
2

To expand on user 190106's answer, this code should give you what you wanted:

function getNextDay(day, resetTime){
  var days = {
    sunday: 0, monday: 1, tuesday: 2,
    wednesday: 3, thursday: 4, friday: 5, saturday: 6
  };

  var dayIndex = days[day.toLowerCase()];
  if (dayIndex !== undefined) {
    throw new Error('"' + day + '" is not a valid input.');
  }

  var returnDate = new Date();
  var returnDay = returnDate.getDay();
  if (dayIndex !== returnDay) {
    returnDate.setDate(returnDate.getDate() + (dayIndex + (7 - returnDay)) % 7);
  }

  if (resetTime) {
    returnDate.setHours(0);
    returnDate.setMinutes(0);
    returnDate.setSeconds(0);
    returnDate.setMilliseconds(0);
  }
  return returnDate;
}

alert(getNextDay('thursday', true));
Community
  • 1
  • 1
brianpeiris
  • 10,735
  • 1
  • 31
  • 44
2

And if you do not want do pass numbers but weekday-names (sunday - saturday) to find a future date of a certain weekday, then this helps you as well:

function getDateOfWeekday(refday){
    var days = {
        monday: 1,
        tuesday: 2,
        wednesday: 3,
        thursday: 4,
        friday: 5,
        saturday: 6,
        sunday: 0
    };
    if(!days.hasOwnProperty(refday))throw new Error(refday+" is not listed in "+JSON.stringify(days));
    var currDate = new Date();
    var currTimestamp = currDate.getTime();
    var triggerDay = days[refday];
    var dayMillDiff=0;
    var dayInMill = 1000*60*60*24;
    // add a day to dayMillDiff as long as the desired refday (sunday for instance) is not reached
    while(currDate.getDay()!=triggerDay){
        dayMillDiff += dayInMill;
        currDate = new Date(currDate.getTime()+dayInMill);
    }
    return new Date(currTimestamp + dayMillDiff);
}

var sunday = getDateOfWeekday("sunday");
document.write("Next Sunday is at: <strong>"+sunday.toLocaleString()+"</strong><br/>");

var thursday = getDateOfWeekday("thursday");
thursday.setHours(0,0,0,0); // set hours/minutes/seconds and millseconds to zero
document.write("Next Thursday is at: <strong>"+thursday.toLocaleString()+"</strong> on midnight<br/>");

var tuesday = getDateOfWeekday("tuesday");
document.write("Next Tuesday is at: <strong>"+tuesday.toLocaleString()+"</strong><br/>");
Blauharley
  • 4,186
  • 6
  • 28
  • 47
0

A swiss-knife tool for javascript DateTime programming.

http://www.datejs.com/

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137