Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Calendar not calculating weekend days

rhi_rej
Level 2
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
_Bruce_Robertson
Level 10
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