Find the date for the nearest day of the week, in this case Fri & Sat General objective: Find the date (day of the month) of the nearest specified day of the week and output.

More specifically: Focusing on events which take place every Fri &amp; Sat, want the page to automatically update dates to next events. HOWEVER for clarity the page should never display Fridays and Saturdays which are NOT part of the same weekend. This would only happen on a SAT. So, if today's date is SAT, the displayed date for FRI should be yesterday (the immediately previous Friday), while the displayed date for SAT should be today's date.

It seems to work now. 

Does this code follow common best practices?

Can you foresee cases where it would fail?

I am quite new to JavaScript and coding as you can probably tell.

(function() {

  var startingDate = new Date();

    function dateOfNearestDay(startingDate, nearestDay) {

    var nearestTime = new Date(startingDate.getTime());

    if (startingDate.getDay() == 6 &amp;&amp; nearestDay == 5) {

      nearestTime.setDate((startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7) - 7);
    } 
    else {

      nearestTime.setDate(startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7);

    }

    var nearestMonth = nearestTime.getMonth();
    var nearestDate = nearestTime.getDate();
    var nearestBoth = [nearestMonth, nearestDate];

    return nearestBoth;
  }

  monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

  var nearestMonthFriday = monthNames[dateOfNearestDay(startingDate, 5)[0]];
  var nearestDateFriday = dateOfNearestDay(startingDate, 5)[1];
  var nearestMonthSaturday = monthNames[dateOfNearestDay(startingDate, 6)[0]];
  var nearestDateSaturday = dateOfNearestDay(startingDate, 6)[1];

  var fridaysDate = document.getElementById('fridaysDate');
  var saturdaysDate = document.getElementById('saturdaysDate');

  fridaysDate.textContent = nearestMonthFriday + " " + nearestDateFriday;    
  saturdaysDate.textContent = nearestMonthSaturday + " " + nearestDateSaturday;

}());
I think dateOfNearestDay would be more intuitive and useful if it would return the date object, 
after all you are dealing with dates here. This would also simplify the caller code, who now works with a date object instead of an array.

Keep a separated method to format the date the way you want to and you are good to go.
Besides of this two things you wrote nice code overall.



(function() {

  var startingDate = new Date();

  function dateOfNearestDay(startingDate, nearestDay) {
    var nearestTime = new Date(startingDate.getTime());

    if (startingDate.getDay() == 6 &amp;&amp; nearestDay == 5) {
        nearestTime.setDate((startingDate.getDate() + 
           (7 + nearestDay - startingDate.getDay()) % 7) - 7);
    } 
    else {
        nearestTime.setDate(startingDate.getDate() + 
           (7 + nearestDay - startingDate.getDay()) % 7);
    }

    return nearestTime;
  }

  function getMonthAndDay(date){
      var monthNames = ['January', 'February', 'March', 'April', 
      'May', 'June', 'July', 'August', 'September', 
      'October', 'November', 'December'];

      return monthNames[date.getMonth()]+ " " + date.getDate(); 
  }
  
  var nearestDateFriday = dateOfNearestDay(startingDate, 5);
  var nearestDateSaturday = dateOfNearestDay(startingDate, 6);

  var fridaysDate = document.getElementById('fridaysDate');
  var saturdaysDate = document.getElementById('saturdaysDate');

  fridaysDate.textContent = getMonthAndDay(nearestDateFriday );
  saturdaysDate.textContent = getMonthAndDay(nearestDateSaturday );

}());
&lt;div id="fridaysDate"&gt;&lt;/div&gt;
&lt;div id="saturdaysDate"&gt;&lt;/div&gt;