Skip to content

Instantly share code, notes, and snippets.

@nikmartin
Last active October 28, 2015 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikmartin/01e484bd2c976503b73a to your computer and use it in GitHub Desktop.
Save nikmartin/01e484bd2c976503b73a to your computer and use it in GitHub Desktop.
This javascript snippet gets the date of the next day requested
function getNextDate(whichDay) {
console.log(whichDay);
var today = new Date(); //Wed Oct 28 2015
var currentDayOfWeek = today.getDay(); //0-6
var thisWeeksSunday = today.getDate() - currentDayOfWeek; //1-31
var theDate = today;
//if whichDay has not happened this week yet:
if (whichDay > currentDayOfWeek) { //then get this weeks whichday
theDate.setDate(thisWeeksSunday + whichDay);
return theDate;
} else { //if we are here, then which day has passed this week, so get next weeks whichDay
theDate.setDate(thisWeeksSunday + 7 + whichDay);
return theDate;
}
}
console.log(getNextDate(0)); //Sun Nov 01 2015
console.log(getNextDate(1)); //Mon Nov 02 2015
console.log(getNextDate(2)); //Tue Nov 03 2015
console.log(getNextDate(3)); //Wed Nov 04 2015
console.log(getNextDate(4)); //Thu Oct 29 2015
console.log(getNextDate(5)); //Fri Oct 30 2015
console.log(getNextDate(6)); //Sat Oct 31 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment