3

Hi want to get a date list between start date and end date. For example start date is 27-08-2010 and end date is 31-08-2010. So the date list is 27-08-2010,30-08-2010 and 31-08-2010. 29-08-2010 and 30-08-2010 will be ignore because it is in the weekend. I attach the picture for more clearer explanation. How to achieve this using javascript or jquery? I only want to get the list of date, for the business day calculation already done.

alt text

cyberfly
  • 5,568
  • 8
  • 50
  • 67

1 Answers1

5

First of all, we can disable in the datepickers the holidays and the weekends, for that we'll be setting it like this in each datepicker component:

Disable weekends and holidays from 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;
}

Now that we have the behavior to not let users select weekends or holidays as startdate or enddates we continue, calculating the total days between those dates:

Calculate Business Dates between two dates

as Find day difference between two dates (excluding weekend days) we have this function to calculate business days between dates:

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
}

Count down Holidays

I'm right now in a hurry, what you need to do now is --iDateDiff (just before the return) for every date in disabledDays that is between dDate1 and dDate2.

How will you accomplish that?... You will iterate over the disabledDays array and convert/parse it to the Date Object and evaluate like:

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

I hope i've helped you...


EDITED:

Sorry, misunderstood the question, try this:

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

Community
  • 1
  • 1
Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
  • sorry i forgot to inform you that i already done the jquery disable weekdays and calculate business day part. Right now i only want to generate the list of date that have been selected. I dont think the Count down Holidays will get me to generate the date list. Or correct me if i wrong. Anyway thanks for your help. – cyberfly Aug 26 '10 at 04:28