How to get the date between the start date and the end date? (excluding weekend date)

Hi, I want to get a list of dates between the start date and the end date. For example, the start date is 08/27/2010 and the end date is 08/31/2010. Thus, the list of dates is 27-08-2010,30-08-2010 and 31-08-2010. 08/29/2010 and 08/30/2010 will be ignored because it is a weekend. I am attaching an image for a clearer explanation. How to achieve this using javascript or jquery? I just want to get a list of dates, for the calculation of the working day has already been done.

alt text

+3
source share
1 answer

, datepickers , , datepicker:

DatePicker

var disabledDays = ["10-22-2010","8-16-2010"];
$("#startDate").datepicker({
    constrainInput: true,
beforeShowDay: noWeekendsOrHolidays
});

function nationalDays(date) {
    var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
    for (i = 0; i < disabledDays.length; i++) {
        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
            return [false];
        }
    }
    return [true];
}
function noWeekendsOrHolidays(date) {
    var noWeekend = jQuery.datepicker.noWeekends(date);
    return noWeekend[0] ? nationalDays(date) : noWeekend;
}

, , startdate enddates, , :

-

as ( ). :

function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
    var iWeeks, iDateDiff, iAdjust = 0;
    if (dDate2 < dDate1) return -1; // error code if dates transposed
    var iWeekday1 = dDate1.getDay(); // day of week
    var iWeekday2 = dDate2.getDay();
    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

    if (iWeekday1 <= iWeekday2) {
      iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
    } else {
      iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
    }

    iDateDiff -= iAdjust // take into account both days on weekend

    return (iDateDiff + 1); // add 1 because dates are inclusive
}

, --iDateDiff ( ) disabledDays, dDate1 dDate2.

?... disabledDays /parse Date :

var holiDay = Date.parse(iteratedDate);
if((holiDay >= dDate1 && holiDay <= dDate2)) {
    --iDateDiff
}

, ...


:

, , :

var Weekday = new Array("Sun","Mon","Tue","Wed","Thuy","Fri","Sat");
while (startDate<=endDate)
  {
  var weekDay = startDate.getDay();
  if (weekDay < 6 && weekDay > 0) {
    var month = startDate.getMonth()+1;
    if( month <= 9 ) { month = "0"+month; }
    var day = startDate.getDate();
    if( day <= 9 ) { day = "0"+day; }
    document.write(day+"/"+month+"/"+startDate.getFullYear() + " ("+Weekday[weekDay]+")<br />");
  }
  startDate.setDate(startDate.getDate()+1)

  }

Live DEMO

+5

Source: https://habr.com/ru/post/1761652/


All Articles