6
\$\begingroup\$

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 & 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 && 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;

}());
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I hope you get some good answers. \$\endgroup\$ – Phrancis Apr 11 '16 at 6:14
1
\$\begingroup\$

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 && 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 );

}());
<div id="fridaysDate"></div>
<div id="saturdaysDate"></div>

\$\endgroup\$
1
\$\begingroup\$

just suggesting !!

maybe its would be better and with less coding to follow this logic :

  1. get the current date (stored to a variable today);
  2. loop includig incrementation to the today VAR until it reaches 5 and 6 (i mean while today != 5 and the same goes for 6);
  3. save them seperately to different variables;
\$\endgroup\$
1
\$\begingroup\$

Date.prototype.addDays = function(days){//  a prototype added to the date
//object to to add any number of days to the current date;
    var dat = new Date(this.valueOf());
	if (days == 0)days=6;
    dat.setDate(dat.getDate() + days);
    return dat;
}
var d = [0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1];
var get_d = function(day1,day2){
     var o = new Date();
     var tod = o.getDay();
     var c1 = 0;
	 var c2 = 0;
     for (var i=tod;i<d.length;i++){
	     if(day1 != d[i])c1+=1;
		 else break;
	 } 	 
     for (var i=tod;i<d.length;i++){
	     if(day2 != d[i])c2+=1;
		 else break;
	 } 	 	 
     alert("1st DAY : "+o.addDays(c1)+"\n 2nd DAY : "+o.addDays(c2));	 
}
get_d(5,6);

here is an alternative solution which i believe it can be heavily improved

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.