1

I am trying to calculate the number of working hours in a month to display on my website. I have managed to figure out how to get the days to display but its just getting the hours for different months considering that some months have 31,30 and 28/29 days.

 function getBusinessDatesCount(startDate, endDate) {
    var count = 0;
    var curDate = startDate;
    while (curDate <= endDate) {
        var dayOfWeek = curDate.getDay();
        if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
           count++;
        curDate.setDate(curDate.getDate() + 1);
    }
    return count;
}

Usage:

var startDate = new Date('7/01/2015');
var endDate = new Date('7/31/2015');
var numOfDates = getBusinessDatesCount(startDate,endDate);
$('div#result').text(numOfDates);
Steven
  • 13
  • 3
  • You need to account for holidays if you want an accurate count of working hours. And besides that all you need in the above code is to multiply the number of workdays by the hours worked in a day( if that's also variable you will have to calculate that for each day as well) – dinotom May 23 '16 at 11:23
  • This doesn't really directly answer your question, but it's probably still worth having a look at the https://date-fns.org library to see how much of this problem has already been solved for you. – Phil.Wheeler May 23 '16 at 12:37

2 Answers2

0

look at this link. This gives u week in month just calculate working day which differ from country to country I think. As in Australia we got 5 working days in a week from there u can multiply it by working hours.

Community
  • 1
  • 1
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
0

As your function is already giving you the count of business day you just need to multiply that with the number of working hours .

function getBusinessDatesCount(startDate, endDate) {
    var count = 0;
    var curDate = startDate;
    while (curDate <= endDate) {
        var dayOfWeek = curDate.getDay();
        if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
           count++;
        curDate.setDate(curDate.getDate() + 1);
    }
    return count;
}

Considering that this function is returning you correct no of days . what you can do is just multiply the no of hours in your country .

If any thing else is required . Please comment I would try to help you out with that .

  • I have to display the total possible working hours for every month. So on the first of each month it needs to change to suite the month. – Steven May 24 '16 at 06:30
  • actually it would be perfect if I could just display it for this current month – Steven May 24 '16 at 07:12