Expand my Community achievements bar.

Calendar not calculating weekend days

Avatar

Level 2

Hi,

I have a holiday form which allows you to enter the first day of annual leave and then the last day of annual leave. It then calculates the total amount of weekdays that you have taken off. The problem that is occuring is that it calualates a monday to friday fine but not if you select a Tuesday on the one week and the the Tuesday on the next it seems to add one extra day in.

Can anyone take a look at the problem for me?

1 Reply

Avatar

Level 10

Hi,

Try this bit of JavaScript;

var Days = { Sunday : 0, Monday : 1, Tuesday : 2, Wednesday : 3, Thursday : 4, Friday : 5, Saturday : 6 };

function getElapsedWorkDays(date1, date2, weekend)

{

    if (weekend === undefined) weekend = [Days.Saturday, Days.Sunday];

    var result = 0;

    var startDate;

    var endDate;

    if (date1.getTime() > date2.getTime())

    {

        startDate = date2;

        endDate = date1;

    }

    else

    {

        startDate = date1;

        endDate = date2;

    }

    for (var currentDate = startDate; currentDate.getTime() < endDate.getTime(); currentDate.setDate(currentDate.getDate()+1))

    {

        if (weekend.indexOf(currentDate.getDay()) < 0)

        {

            result++;

        }

    }

    return result;

}

var workingDays = getElapsedWorkDays(new Date(2013,06,18), new Date(2013,06,25));

console.println(workingDays);

Regards

Bruce