简体   繁体   中英

Calculate Business Days in Month with Javascript

I'm trying to do 2 things.

  1. Calculate number of business days to date in a given month based on current day (ie today is March 7, 2012 therefore, 5 business days have passed)
  2. Calculate number of business days to go in a given month based on current day (ie Today is March 7, 2012 therefore, there are 17 business days left this month.

Any help here would be much appreciated.

EDIT: This is what I've tried so far:

function isWeekday(year, month, day) {var day = new Date(year, month, day).getDay();return day !=0 && day !=6;}
function getWeekdaysInMonth(month, year) {var days = daysInMonth(month, year);var weekdays = 0;for(var i=0; i< days; i++) {if (isWeekday(year, month, i+1)) weekdays++;}return weekdays;}
function calcBusinessDays(dDate1, dDate2) {
    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
}

Not quite sure how to put it all together to get business days passed and business days to go.

Here's a really simple function that just loops over the days, should be fast enough as it should never have to loop more than 31 times. If the current day is a business day, it's counted in the days past:

function businessDays(date) {

  // Copy date
  var t = new Date(date);
  // Remember the month number
  var m = date.getMonth();
  var d = date.getDate();
  var daysPast = 0, daysToGo = 0;
  var day;

  // Count past days
  while  (t.getMonth() == m) {
    day = t.getDay();
    daysPast += (day == 0 || day == 6)? 0 : 1;
    t.setDate(--d);
  }

  // Reset and count days to come
  t = new Date(date);
  t.setDate(t.getDate() + 1);
  d = t.getDate();

  while  (t.getMonth() == m) {
    day = t.getDay();
    daysToGo += (day == 0 || day == 6)? 0 : 1;
    t.setDate(++d);
  }
  return [daysPast, daysToGo];
}

alert(businessDays(new Date(2012,2,7))); // 7-Mar-2012 => 5, 17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM