Skip to content

Instantly share code, notes, and snippets.

# davidsk/WeekOfDay Count Calculator.js Last active Sep 20, 2016

WeekOfDay Count Calculator created by davidsk - https://repl.it/DFqJ/25
 var DayOfWeek = { Sunday: 0, Monday: 1, Tuesday: 2, Wednesday: 3, Thursday: 4, Friday: 5, Saturday: 6 }; function CountWeekdayInPeriod(targetDayIndex, startDayIndex, periodDuration){ // ensure values are integers; js will concatenate string values startDayIndex = parseInt(startDayIndex, 10), periodDuration = parseInt(periodDuration, 10); // find the number of days in addition to whole weeks var partialWeekLagDayCount = periodDuration % 7; // whichever day is being targeted, there is always one instance every 7 days var dayCount = (periodDuration - partialWeekLagDayCount) / 7; // determine the start day index having 'rotated' the days so that the target day is now index 7 var firstDayIndex = ((startDayIndex + (6 - targetDayIndex)) % 7 ) + 1 // calculate the end date's day index by adding the period to the adjusted start date index // 1 is deducted as the start date already considered var firstDayPlusDurationIndex = firstDayIndex + partialWeekLagDayCount - 1; // determine the weekday index of the last day of the remaining period var lastWeekdayIndex = (firstDayPlusDurationIndex % 7); // determine whether the remaining period has 'gone around the horn' var secondIteration = (firstDayPlusDurationIndex - lastWeekdayIndex) / 7; dayCount += secondIteration; return dayCount; } // functions built on top of CountWeekdayInPeriod function CountWeekendDaysInPeriod(startDayIndex, periodDuration) { return CountWeekdayInPeriod(DayOfWeek.Saturday, startDayIndex, periodDuration) + CountWeekdayInPeriod(DayOfWeek.Sunday, startDayIndex, periodDuration); } function GetNextWeekday(startDate, periodDuration){ var weekendDayCount, totalWeekendDays = 0, iterativePeriodDuration = periodDuration; if(!iterativePeriodDuration || iterativePeriodDuration === 0){ iterativePeriodDuration = 1; } var direction = iterativePeriodDuration / Math.abs(iterativePeriodDuration); var internalStartDate = new Date(startDate); if(direction === -1){ internalStartDate.setDate(internalStartDate.getDate() + iterativePeriodDuration); } var startDayIndex = internalStartDate.getDay(); do{ weekendDayCount = CountWeekendDaysInPeriod(startDayIndex, Math.abs(iterativePeriodDuration)); iterativePeriodDuration = weekendDayCount; startDayIndex = (7 + startDayIndex + (direction * (iterativePeriodDuration % 7))) % 7; totalWeekendDays += weekendDayCount; } while(weekendDayCount > 0); var totalDelta = periodDuration + (direction * totalWeekendDays); return new Date(new Date(startDate).setDate(startDate.getDate() + totalDelta)); } // usage var weekdaysInPeriod = CountWeekdayInPeriod(DayOfWeek.Wednesday, DayOfWeek.Tuesday, 10); console.log("weekdaysInPeriod: " + weekdaysInPeriod); var weekendDaysInPeriod = CountWeekendDaysInPeriod(DayOfWeek.Tuesday, 11); console.log("weekendDaysInPeriod: " + weekendDaysInPeriod); var nextWorkingDay = GetNextWeekday(new Date(2016, 8, 24), 0); console.log("nextWorkingDay: " + nextWorkingDay);
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.