开发者

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

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 cleare开发者_开发问答r 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.

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜