# fin de semana, las vacaciones cuentan entre dos fechas sin iteración

i have to calculate the weekend Count and holidays count between the following two dates.

``````var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var holidays = [new Date("01/06/2014"), new Date("01/26/2014")];
``````

preguntado el 28 de enero de 14 a las 18:01

Entonces, ¿cuál es tu pregunta? -

Check out Momentjs (momentojs.com) or Sugarjs (sugarjs.com/dates) -

without iteration, you mean not iterating from startDate to endDate? -

i have check this moment but i can't bind the logic for this weekend count calculation -

yes Mr.J.Rahmati is it possible to calculate without iteration between two different date; -

## 3 Respuestas

This loops through a partial week, so there are at most 6 iterations. I can't think of a more elegant way to solve that with pure JS.

``````var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");

var diff = Math.abs(startDate - endDate); // in milliseconds
var ms_per_day = 1000*60*60*24;
var days = diff/ms_per_day + 1; // convert to days and add 1 for inclusive date range
var mod = days % 7;
var full_weeks = (days - mod) / 7;

var weekend_days = full_weeks * 2;

if (mod != 0) { // iterate through remainder days
var startPartialWeek = new Date();
var endPartialWeek = endDate;
startPartialWeek.setTime(endDate.getTime() - (mod - 1)*ms_per_day);
for (var d = startPartialWeek; d <= endPartialWeek; d.setDate(d.getDate() + 1)) {
if(d.getDay() == 0 || d.getDay() == 6) {
weekend_days++;
}
}
}

``````

This counts Saturdays and Sundays only, not holidays. I don't think you'll be able to do holidays without iterating through a collection of holiday dates you get from some other source.

Respondido el 28 de enero de 14 a las 19:01

Having following two functions:

``````function calculateTotalDays(firstDate, secondDate){
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
return diffDays;
}

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
}
``````

You can calculate total weekenddays as follows:

``````var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var totalDays = calculateTotalDays(startDate, endDate);
var weekendDays = totalDays - calcBusinessDays(startDate, endDate);
``````

And then count holidays in between start and endDate:

``````var totalHolidays = 0;
for (var i = 0, i < holidays.length; i++){
var d = holidays[i].getDay();//Make sure holiday is not a weekendday!
if (holidays[i] >= startDate && holidays[i] <= endDate && !(d == 0 || d==6))
totalHolidays++;
}
``````

Respondido el 28 de enero de 14 a las 20:01

As additional info calcBusinessDays is the function from this página! I didn't write it myself! - J. Rahmati

This fails if the start day is a "sunday" and the end one is a "friday". It only counts 1 day. For example, from the 18th of January of 2015 to the 23rd of January of 2015. Here's the reproduction of the bug - Alvaro

``````// Count days between the 2 dates
var days = Math.floor(((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())
- Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (24 * 60 * 60 * 1000)));

// Count holidays
var countHolidays = 0;
for (var i = 0, len = holidays.length; i < len; ++i)
if (holidays[i] >= startDate && holidays[i] <= endDate)
++countHolidays;

// Vars used to count sundays and saturdays
var adjustingDays1 = (7 - startDate.getDay()) % 7, // days between week of startDate and sunday
adjustingDays2 = (7 + 6 - startDate.getDay()) % 7, // days between week of startDate and saturday
oddDays = days % 7; // remainder of total days after dividing 7
completeWeeks = Math.floor(days / 7);

// Count weekend days
var countWeekEndDays = completeWeeks + (oddDays >= adjustingDays1 ? 1 : 0) +
completeWeeks + (oddDays >= adjustingDays2 ? 1 : 0);

console.log('holidays: ' + countHolidays);
console.log('weekend days: ' + countWeekEndDays);
``````

Respondido el 28 de enero de 14 a las 20:01

This can give back non-integer values for `countWeekEndDays`. Ejemplo: `Date("2013/12/29")` a `Date("2014/01/21")`. - Natalie Chouinard

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.