javascript - How to check if days between two dates are weekdays? -
i have form user take 2 dates calendar. want check if days between 2 dates user selected in friday monday (include).
i found script count weekdays (days include satuday , sunday):
function calcbusinessdays (ddate1, ddate2) { var iweeks, idatediff, iadjust = 0; if (ddate2 < ddate1) return 0; var iweekday1 = ddate1.getday(); var iweekday2 = ddate2.getday(); iweekday1 = (iweekday1 == 0) ? 7 : iweekday1; // change sunday 0 7 iweekday2 = (iweekday2 == 0) ? 7 : iweekday2; if ((iweekday1 > 5) && (iweekday2 > 5)) iadjust = 1; // adjustment if both days on weekend iweekday1 = (iweekday1 > 5) ? 5 : iweekday1; // 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 account both days on weekend return (idatediff + 1); // add 1 because dates inclusive }
how modify include friday , monday?
just put real quick should work. had trouble understanding sample code. thought might work little better.
var calcbusinessdays = function (ddate1, ddate2) { //we working time stamps var = ddate1.gettime() , = ddate2.gettime() , tempdate = new date() , count = 0; //loop through each day between dates 86400000 = 1 day for(var _from = from; _from < to; _from += 86400000){ //set day tempdate.settime(_from); //if weekend add 1 count if ((tempdate.getday() <= 1) || (tempdate.getday() >= 5)) { count++; } } //return count =) return count; }
this add 1 friday, saturday, sunday , monday. line need changed if wanted other days if statement nested in loop.
Comments
Post a Comment