@davidsk/

DayOfWeek Count Calculator

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var DayOfWeek = {
    Sunday: 0,
    Monday: 1,
    Tuesday: 2,
    Wednesday: 3,
    Thursday: 4,
    Friday: 5,
    Saturday: 6
};
    
function getDayIndex(startDayIndex, duration){
	return (startDayIndex + ((7 + duration) % 7)) % 7;
}

function CountWeekdayInPeriod(targetDayIndex, startDayIndex, periodDuration){

	var log = 0;

    if (log) console.log("			targetDayIndex: " + targetDayIndex);

    // 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;
    if (log) console.log("			partialWeekLagDayCount: " + partialWeekLagDayCount);

    // whichever day is being targeted, there is always one instance every 7 days
    var dayCount = (periodDuration - partialWeekLagDayCount) / 7;    
    if (log) console.log("			dayCount: " + dayCount);

    // determine the start day index having 'rotated' the days so that the target day is now index 7
    var firstDayIndex = (startDayIndex + (6 - targetDayIndex)) % 7
    if (log) console.log("			firstDayIndex: " + firstDayIndex);
    
    // 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;
    if (log) console.log("			firstDayPlusDurationIndex: " + firstDayPlusDurationIndex);

    // determine the weekday index of the last day of the remaining period
    var lastWeekdayIndex = (firstDayPlusDurationIndex % 7);
    if (log) console.log("			lastWeekdayIndex: " + lastWeekdayIndex);
    
    // determine whether the remaining period has 'gone around the horn' 
    var secondIteration = (firstDayPlusDurationIndex - lastWeekdayIndex) / 7;
    if (log) console.log("			secondIteration: " + secondIteration);
    
    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);
    
    console.log("direction: " + direction);
    
    if(direction === -1){
         internalStartDate.setDate(internalStartDate.getDate() + (iterativePeriodDuration));
    }

    var startDayIndex = internalStartDate.getDay();
    
	var iteration = 0;

    do{
        weekendDayCount = CountWeekendDaysInPeriod(startDayIndex, Math.abs(iterativePeriodDuration));
        
		console.log("CountWeekendDaysInPeriod(" + startDayIndex + ", " + Math.abs(iterativePeriodDuration) + ") = " + weekendDayCount);

		if(direction === 1){
			startDayIndex = (startDayIndex + iterativePeriodDuration + 1) % 7;	
		}

        iterativePeriodDuration = weekendDayCount;

		if(direction === -1){
			startDayIndex = startDayIndex + 7 - iterativePeriodDuration
		}

        totalWeekendDays += weekendDayCount;

		iteration++;

    } while(weekendDayCount > 0);
    
    var totalDelta = periodDuration + (direction * totalWeekendDays);

    return new Date(new Date(startDate).setDate(startDate.getDate() + totalDelta));
}

// usage
//var weekdaysInPeriod = CountWeekdayInPeriod(DayOfWeek.Saturday, DayOfWeek.Tuesday, 5);
//console.log("weekdaysInPeriod: " + weekdaysInPeriod);

var nextWorkingDay = GetNextWeekday(new Date(2016, 8, 19), 7);
console.log("nextWorkingDay: " + nextWorkingDay);


Native Browser JavaScript