Javascript Snippet: Find the next day and time

This will allow you to find the next weekday and time from another time (by default the current date). For example, this can be used to find the Date of the next Monday at 6:30am.

var d = new Date();
var nextD = 1; // Monday
var nextT = 390; // 6 hours 30 minutes
if (d.getDay() == nextD)
{
  if ((d.getHours() * 60 + d.getMinutes()) >= nextT)
    {
      d.setDate(d.getDate() + 7);
    }
}
else if (d.getDay() < nextD)
{
  d.setDate(d.getDate() + (d.getDay() - nextD));
}
else
{
  d.setDate(d.getDate() + nextD + (7 - d.getDay()));
}
d.setHours(0,nextT,0,0);

alert(d);

Optimisations are always welcome in the comments section.


Code Snippets
Wed, 29 Jul 2009 14:50