0

Below I've written a function that works out how many week days and how many weekend days their are between two given dates and stores them in an array. However, They arn't giving the right results. I cant figure out why. Any pointers?

The array is returning [7,0,7] instead of [5,2,7]

    $(document).ready(function(){

            var a = new Date(2014, 05, 12);
            var b = new Date(2014, 05, 19);


            var d = getDays(a, b);
            var e = getDays(a, b);

            console.log(e);
            console.log(d);

            function getDays(a, b){
                var days = new Array();

                days[0] = 0;
                days[1] = 0;
                days[2] = 0;

                var x = dayDiff(a, b);

                var c = new Date(a.getDate());

                for(var i = 0; i < x; i++) {
                    c.setDate(a.getDate() + 1);


                    switch(c.getDay()){
                        case 0:
                        case 6:
                            days[1] += 1;
                        break
                        case 1:
                        case 2:
                        case 3: 
                        case 4:
                        case 5:
                            days[0] += 1;
                        break;

                    }

                }

                days[2] = days[1] + days[0];

                return days;
            }

            function dayDiff(a, b) {
                return Math.round((b-a)/(1000*60*60*24));
            }



        });
  • I would think you could get the number of days in period, divide by seven, multiple by two, and fix for the first/last two days being weekends (maybe only if there's not a round number). And then tweak to get it right. – Jared Farrish Jun 13 '14 at 20:39
  • Are you taking into account the day of the week that the date occurs upon? Two dates may have 10 days between them, but could contain up to two weekends, or only one weekend. – Dpeif Jun 13 '14 at 20:40
  • The array is returning [7,0,7] instead of [5,2,7] –  Jun 13 '14 at 20:41
  • 1
    Use a library, there are too many edge cases to waste your life trying to calculate these things yourself! – Julian Knight Jun 13 '14 at 20:42
  • @JaredFarrish I was thinking the same thing... – user3334690 Jun 13 '14 at 20:43
  • Because of the system that it's going on, I cannot use another library unfortunately. –  Jun 13 '14 at 20:43
  • There are quite a few proven answers: http://stackoverflow.com/questions/6210906/how-to-determine-number-saturdays-and-sundays-comes-between-two-dates-in-java-sc and http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days?rq=1 – Alex Pakka Jun 13 '14 at 20:44
  • Say Date A is Jan/1/2000 and Date B is Jan/10/2000. There are ten "days" between those two dates but without looking at a calendar you don't know what days of the week they are. If it's a "Monday to next Thursday", then there are 8 weekdays and 2 weekend days between A and B. If it's a "Saturday to next Tuesday", then there are 4 weekend days and 6 weekdays between A and B. IMO, depends on how you want to locate that day-of-the-week value before you figure out how to attack the calculation. – Dpeif Jun 13 '14 at 20:51

1 Answers1

0

This line is incorrect:

c.setDate(a.getDate() + 1);

It should be:

c.setDate(c.getDate() + 1);

Since you are never changing the date of a, each iteration is using the same date.

Edit: Also, this line looks incorrect:

var c = new Date(a.getDate());

What you want is this:

var c = new Date(a);

.getDate() returns only the date-number within the month.

Robert Messerle
  • 3,022
  • 14
  • 18