calculate business hours between two dates but limit each day to 8 hours

Answered

Comments

4 comments

  • ESDC

    Any help would be appreciated. Thanks :)

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    Hi,

    this should work:

    var d1 = moment(@Start);
    var d2 = moment(@End);
    
    var hours = d2.diff(d1, 'hours');
    
    var nwhrs = 16;
    var timeCorrect = parseFloat(moment(d2).tz("Europe/Berlin").format("ZZ"))/100; 
    
    var lunchCheck = moment(d2).hours(12).subtract(timeCorrect, 'h');
    var dayFactor = Math.floor(hours/24);
    var lunchFactor = d2 <= lunchCheck ? 0 : 1;
    
    hours-(nwhrs*dayFactor)-lunchFactor 
    

    nwhrs = non work hours per day;
    timeCorrect could be necessary. You have to take your time zone (instead of my time zone 'Europe/Berlin'). If you get wrong results with timeCorrect just disable it.
    This formula includes the lunch break. So when your start = March 1 8am and end = March 2 14 pm the result should be 13 business hours, The formula is only usable for a workday with 8 business hours and a lunchbreak with 1 hour.

    Rainer
    rg@delos-consulting.com

    1
    Comment actions Permalink
  • ESDC

    Wow thanks Rainer ... this worked perfectly! :D

    0
    Comment actions Permalink
  • Jayraj Berde

    wow Rainer's solution worked perfectly. 

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk