hawx
hawx

Reputation: 1679

Getting Working Days Using Javascript

I'm trying to get the working days between two date. Example: stdate = 28/10/2011 and endate = 04/11/2011. This should be 6 working days, but its only giving 5 days.

var workingdays = 0;
var weekday     = new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

while (stdate <= endate) 
{
    var day = weekday[stdate.getDay()];
    if(day != "Saturday" && day != "Sunday") 
    {
        workingdays++; 
    }
    console.log(weekday[stdate.getDay()]);
    stdate = new Date(stdate.getTime() + 86400000); 
}

The console log shows the results below.

Friday
Saturday
Sunday
Sunday
Monday
Tuesday
Wednesday
Thursday

Sunday shows twice for some reason. Any help would be appreciated.

Upvotes: 9

Views: 27249

Answers (4)

Binh LE
Binh LE

Reputation: 377

You can get Date of week from StartDate and EndDate

var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var object = [];


    function getTuesday(startDate, endDate, n) {

        var currentDate = startDate;
        while (currentDate <= endDate) {
            if (weekday[currentDate.getDay()] === weekday[n])
                object.push(currentDate + "\n");
            currentDate.setDate(currentDate.getDate() + 1);
        } return object;
    }

    var begin = new Date(2015, 11, 08);

    var end = new Date(2016, 01, 08);

    alert(getTuesday(begin, end, 3) + "\n Total date or week: " + object.length);

Upvotes: 0

Bas Slagter
Bas Slagter

Reputation: 9929

Here you go:

 function getWorkingDays(startDate, endDate){
     var result = 0;

    var currentDate = startDate;
    while (currentDate <= endDate)  {  

        var weekDay = currentDate.getDay();
        if(weekDay != 0 && weekDay != 6)
            result++;

         currentDate.setDate(currentDate.getDate()+1); 
    }

    return result;
 }

 var begin = new Date(2011, 09, 8);
 var end = new Date(2011, 09, 25);
 alert(getWorkingDays(begin, end)); // result = 12 days

Keep in mind that the month indication for the two variables is zero based. So in my example we are looking at october (month 10).

Upvotes: 12

Ankur
Ankur

Reputation: 12784

You can try this

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

in place of

stdate = new Date(stdate.getTime() + 86400000); 

Upvotes: 2

Guffa
Guffa

Reputation: 700910

Daylight savings time.

As you are adding 24 hours to the date, it's not enough to get you to the next day on the sunday, as that particular sunday has 25 hours.

You should add a day instead of adding hours:

stdate = new Date(stdate.getFullYear(), stdate.getMonth(), stdate.getDate() + 1);

Explanation: When you call the Date constructor with a date that is out of range, it will automaticall wrap to the next month, i.e. new Date(2010,9,32) gives you the first of November.

Upvotes: 6

Related Questions