H30
H30

Reputation: 124

counting only working hours in javascript

I'm currently trying to count hours. Excluding outside business hours, weekends and bank hol's

bHols = [........]

function calc_hours_open(ps_mx) {
    var hoursOpen = 0
    var current = ps_mx

    //Working Hours
    var workStart = 8;
    var workStop = 18;
    var d = new Date(current)
    var endDate = new Date()

    while(current <= endDate) {          
        let currHour = d.getHours()
        let currDay = current.day()         

        if(currHour >= workStart && currHour < workStop && 
            currDay !== 0 && currDay !== 6 && 
            bHols.indexOf(current.format('DD/MM/YYYY')) == -1) {
            hoursOpen++;
        }

        current = current.add(1, 'hours')
    }

    return hoursOpen
}

Problem is, it is adding 24 hour not just the 10 working hours between 8 and 18 so when it should only be 20 hrs I'm getting 48.

example

ps_mx = 10/10/2017 11:30:00 bHols = [........]

function calc_hours_open(ps_mx) {
    var hoursOpen = 0
    var current = ps_mx

    //Working Hours
    var workStart = 8;
    var workStop = 18;
    var d = new Date(current)
    var endDate = new Date()
    console.log(d)
    console.log(endDate)
    while(current <= endDate) {          
        let currHour = d.getHours()
        let currDay = current.day()
        console.log(currHour)         
        console.log(currDay)
        if(currHour >= workStart && currHour < workStop && 
            currDay !== 0 && currDay !== 6 && 
            bHols.indexOf(current.format('DD/MM/YYYY')) == -1) {
            hoursOpen++;
        }

        current = current.add(1, 'hours')
    }

    return hoursOpen
}

console.log(hoursOpen) where function is used

logs in order

10/10/2017 11:30:00

17/10/2017 14:00:00

11

1(increments for each day)

147

Upvotes: 0

Views: 391

Answers (2)

H30
H30

Reputation: 124

fixed

bHols = [........]

function calc_hours_open(ps_mx) {
var hoursOpen = 0
var current = ps_mx

//Working Hours
var workStart = 8;
var workStop = 18;
var d = moment(current)
var endDate = moment()

while(d <= endDate) {          
    let currHour = d.hour()
    let currDay = d.day()         

    if(currHour >= workStart && currHour < workStop && 
        currDay !== 0 && currDay !== 6 && 
        bHols.indexOf(current.format('DD/MM/YYYY')) == -1) {
        hoursOpen++;
    }

    d = d.add(1, 'hours')
}

return hoursOpen

}

Upvotes: 0

Leonardo Alves Machado
Leonardo Alves Machado

Reputation: 2837

I believe I found the problem

your check, inside your if statement, is testing if the current date is inside the working hours. The change should be in this part of your code:

let currHour = d.getHours() //d, in your code, is new Date()

if(currHour >= workStart && currHour < workStop && 
        currDay !== 0 && currDay !== 6 && 
        bHols.indexOf(current.format('DD/MM/YYYY')) == -1)

I believe you should get currHour from the current - which is what you are testing (and incrementing in the loop)... d.getHours() never changes in the loop...

Upvotes: 1

Related Questions