Last active
October 28, 2015 21:51
-
-
Save nikmartin/01e484bd2c976503b73a to your computer and use it in GitHub Desktop.
This javascript snippet gets the date of the next day requested
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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