Visokio website     Downloads     Video tutorials     KnowledgeBase  
Formulae: Date difference excluding weekends? - Visokio Forums
Formulae: Date difference excluding weekends?
  •     shaji_o August 23, 2012 1:39AM
    Is there a way to find the number of days between 2 dates, excluding weekends?
  • 4 Comments
  •     shaji_o August 23, 2012 2:18AM
    I can use averages and multiply the difference in days by 5/7 (weekdays/total days in the week). But that won't be 100% correct. Hopefully it isn't too complex to get the correct answer.
  •     steve August 23, 2012 7:03AM
    If you aren't concerned with country-specific holidays, and simply need the number of Mon-Fri days between two dates, this can currently be achieved using Javascript and the SCRIPT formula function (2.7+).

    See attached IOK file which includes the formula and provides a series of test cases.

    (If, on the other hand, you needed to factor in public holidays, you would need a much more sophisticated solution, involving lookup files by country.)
    Attachments
    Working day diff.iok 7K
  •        CRead August 23, 2012 7:10AM
    I tried this a while back unsuccessfully, copying the script here:

    http://partialclass.blogspot.co.uk/2011/07/calculating-working-days-between-two.html

    SCRIPT(
    `
    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;
    }

    workingDaysBetweenDates(start, end) ;

    `,
    'start',[Start Date],
    'end',[End Date])


    I get problems with the javascript results for some reason. To demonstrate this, running the script below returns false!

    SCRIPT(
    `
    start < end
    `,
    'start',TEXTTODATE('2012-08-01','yyyy-MM-dd'),
    'end',TEXTTODATE('2012-08-23','yyyy-MM-dd'))

    whereas the code below returns true:

    SCRIPT(
    `
    var start = new Date("August 01, 2012 00:00:01")
    var end = new Date("August 23, 2012 00:00:01")

    start < end

    `)

    To get around this, pass the dates as text using date, and convert them in JS to dates using the code above:

    SCRIPT(
    `
    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;
    }
    var startd = new Date(start)
    var endd = new Date(end)

    workingDaysBetweenDates(startd, endd) ;

    `,
    'start',DATETOTEXT([Start Date],'MMMM dd, yyyy hh:mm:ss'),
    'end',DATETOTEXT([End Date],'MMMM dd, yyyy hh:mm:ss'))

    I'll post the file in the demos section as well.
  •     shaji_o August 23, 2012 8:22PM
    I wasn't concerned about public holidays, so what you provided Steve was sufficient. Thanks to you both
This discussion has been closed.
← All Discussions

Welcome!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership