Skip to content

Instantly share code, notes, and snippets.

@rhysbrettbowen
Last active December 22, 2015 20:59
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 rhysbrettbowen/6529733 to your computer and use it in GitHub Desktop.
Save rhysbrettbowen/6529733 to your computer and use it in GitHub Desktop.
how to get next friday
tricky one, but doable. You do want to get the number day, so Zuojiang is close, but setting date may not work because of the end of the month...
so get the day:
var date = new Date();
var day = date.getDay();
now we need to normalize those numbers to see how many days forward we need to move. Friday is day 5, but we really want it at day 7 (or 0). so we add 2 and take the modulus of 7.
var normalizedDay = (day + 2) % 7;
now we have the days of the week where we want them. the number of days to go forward by will be 7 minus the normaized day:
var daysForward = 7 - normalizedDay;
if you don't want friday to skip to the next friday then take the modulus of 7 again. Now we just add that many days to the date:
var nextFriday = new Date(+date + (daysForward * 24 * 60 * 60 * 1000));
if you want the start of the day then you have to turn back the hours, minutes and milliseconds to zero:
nextFriday.setHours(0);
nextFriday.setMinutes(0);
nextFriday.setMilliseconds(0);
or for the lazy:
jumpToNextFriday = function(date) {
return new Date(+date+(7-(date.getDay()+2)%7)*86400000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment