JavaScript application for showing the weekend dates?

757 Views Asked by At

I thought a lot - I tried but I could not solve it. I need a JavaScript application that shows the nearest weekend dates in the current date.

If it's a weekend now, give it the dates of this weekend, if not, then next weekend's dates.

I'm waiting for your help. Respects.

3

There are 3 best solutions below

7
On BEST ANSWER

You can use the built-in Date constructor.

var date = new Date();
var day = date.getDay();
var saturday;
var sunday;
if(day === 0 || day === 6){ //0 for Sunday, 6 for Saturday
    saturday = date;
    sunday = new Date(saturday.getTime());
    sunday.setDate(saturday.getDate() + (day === 0 ? -1 : 1));
    if(day === 0){
        var temp = saturday;
        saturday = sunday; //Confusing, but they are actually the wrong dates, so we are switching the dates
        sunday = temp;
        temp = null; //Free up some memory!
    }
        
}
else{
    //This is the complicated part, we need to find when is the next Saturday
    saturday = new Date(date.getFullYear(), date.getMonth(), (date.getDate() + 6) - day);
    sunday = new Date(saturday.getTime());
    sunday.setDate(saturday.getDate() + (saturday.getDay() === 0 ? -1 : 1));
}
date = day = null; //Free up some memory!
document.body.innerText = [saturday, sunday];

To get the date, use saturday.getDate() or sunday.getDate().Remember that Date months are 0-based. See here for more info.

0
On

I interpret the "nearest" weekend as being the previous weekend for Monday and Tuesday, and the next weekend for Thursday and Friday. You didn't provide any information on what to do with Wednesday.

However, from other answers it seems you want either the current weekend for Saturday and Sunday and or the next weekend for weekdays.

The following is a little more concise than other answers:

/* Get nearest weekend to the provided date
** @param {Date} date - date to get weekends nearst to
** @returns {Array} array of Dates [Saturday, Sunday]
*/
function getNearestWeekend(date) {
  // Copy date so don't mess with provided date
  var d = new Date(+date);
  // If weekday, move d to next Saturday else to current weekend Saturday
  if (d.getDay() % 6) {
    d.setDate(d.getDate() + 6 - d.getDay());
  } else {
    d.setDate(d.getDate() - (d.getDay()? 0 : 1));
  }
  // Return array with Dates for Saturday, Sunday
  return [new Date(d), new Date(d.setDate(d.getDate() + 1))]
}

// Some tests
[new Date(2017,0,7),  // Sat 7 Jan
 new Date(2017,0,8),  // Sun 8 Jan
 new Date(2017,0,9),  // Mon 9 Jan
 new Date(2017,0,12)  // Thu 12 Jan
].forEach(function(d) {
  var opts = {weekday:'short', day:'numeric', month:'short'};
  console.log('Date: ' + d.toLocaleString('en-GB',opts) + ' | Next weekend: ' +
              getNearestWeekend(d).map(d =>d.toLocaleString('en-GB',opts)).join(' and ')
  );
});
 

0
On
 var chosenDay = new Date();
    var box = [];
    var counting = 0;
    for (var i = 0; i < 7; i++) {
      chosenDay.setDate(chosenDay.getDate() + counting);
      var day = chosenDay.getDate();
      var dayy = chosenDay.getDay();
      var month = chosenDay.getMonth()+1;
      var year = chosenDay.getFullYear();
      box.push({day: day, dayy: dayy});
      counting = 1;
    };

Now to find Saturday and Sunday

box.map(function(obj) {
 if (obj.dayy === 6) {
  console.log('Saturday found');
  alert(obj.day);
}; 
 if (obj.dayy === 0) {
  console.log('Sunday found');
  alert(obj.day);
};
});