Calculate difference between to dates / business days

Comments

8 comments

  • Rainer Grabowski
    Change the last line to: 

    startDate == null || endDate == null ? "" : workingDaysBetweenDates( new Date(startDate) , new Date(endDate))

    1
    Comment actions Permalink
  • Rainer Grabowski

    Hi Damir,

    the code seems ok. Can you please share a screen shot of your calculation field and the start and end date for the item where the result is shown?

    Rainer

    0
    Comment actions Permalink
  • Damir Djordjevic

    when fields are empty it shows 1 as result, which is not good because I added this field on existing app

    0
    Comment actions Permalink
  • Damir Djordjevic

    Thanks Rainer, it works. :)

    0
    Comment actions Permalink
  • Jeff Dougherty

    Question, How would I change this to calculate the number of Hours instead of Days?

    0
    Comment actions Permalink
  • Rainer Grabowski

    For the exact same use case it's simply

    workingDaysBetweenDates( new Date(startDate) , new Date(endDate)) * 24 

    If you have another use case you maybe need another calculation. 

     

    0
    Comment actions Permalink
  • Jeff Dougherty

    Thanks Rainer, It is currently calculating the total number of days, but what if they took leave (for leave request) from 2:00 until 5:00. That would be a total of 3 hours. If they missed the following day, it would be 11 hrs. (for 8 hr work day).

    0
    Comment actions Permalink
  • Silvia Tejeda

    This is an old thread, but should this code still work?

    I'm having issues counting the number of business days between two dates. I tried the same code but it says 'the results is not a valid number'.

     @todaysDate is a calculation that update at 12:15am every day, is it possible that that's the issue?

    var startDate = @Todays Date
    var endDate = @Current Auction Date

    function workingDaysBetweenDates(startDate, endDate) {

    // Validate input
    if (endDate < startDate)
    return 0;

    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1); // Start just after midnight
    endDate.setHours(23,59,59,999); // End just before midnight
    var diff = endDate - startDate; // Milliseconds between datetime objects
    var days = Math.ceil(diff / millisecondsPerDay);

    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    var days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();

    // Remove weekend not previously removed.
    if (startDay - endDay > 1)
    days = days - 2;

    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
    days = days - 1

    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
    days = days - 1

    return days;
    }

    startDate == null || endDate == null ? "" : workingDaysBetweenDates( new Date(startDate) , new Date(endDate))

     

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk