Calculate durations between 2 dates

Comments

3 comments

  • Rainer Grabowski

    Hi Nicolas,

    you have to pull @min of dates from A and @min of dates from B, then compare them, which is the older one. Same with @max of dates from C and D. Then calculate the difference between min and max.

    var minA = @min of datefield in App A;
    var minB = @min of datefield in App B;
    var maxC = @max of datefield in App C;
    var maxD = @max of datefield in App D;
    
    var resultMin = minA < minB ? minA : minB;
    var resultMax = maxC > maxD ? maxC : maxD;
    
    var min = moment(resultMin);;
    var max = moment(resultMax);
    
    max.diff(min, "hours")/24
    

    Instead of "hours"/24 you could take "days", but "hours/24 is more exact.

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink
  • Nicolas Roegiers

    Hi Rainer,

    Thanks a lot for the answer. It works but only when i have several items referred to in the first relationship field and several items in the second relationship field. When at least one of the 2 relationship fields has only 1 item referred to it shows no value. Do you know how to solve this issue? Most of the items in app1 have only 1 item in both relationship fields.

    Thanks again,

    Nicolas

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Nicolas,

    this one should work in all cases: If one of the relationships fields has only one value or no value and also if both fields have no value. If one field or both fields have no value the result is 0 (can be changed as you like it).

    var minA = @min of datefield in App A;
    var minB = @min of datefield in App B;
    var maxC = @max of datefield in App C;
    var maxD = @max of datefield in App D;
    
    var minA1 = minA!= null ? moment(minA).format("YYYYMMDD") : "";
    var minB1 = minB != null ? moment(minB).format("YYYYMMDD") : "";
    var AB = (minA1 + minB1).match(/.{1,8}/g);
    
    var maxC1 = maxC != null ? moment(maxC).format("YYYYMMDD") : "";
    var maxD1 = maxD != null ? moment(maxD).format("YYYYMMDD") : "";
    var CD= (minC1+minD1).match(/.{1,8}/g);
    
    var oldest1 = AB != null ? AB.sort()[0] : "";
    var newest2 = CD!= null ? CD.sort(function(a,b){return b-a})[0] : "";
    
    var date1 = oldest1 != "" ? moment(oldest1,"YYYYMMDD") : "";
    var date2 = newest2 != "" ? moment(newest2, "YYYYMMDD") : "";
    
    date1 != "" && date2 != "" ? date2.diff(date1, "days") : 0
    

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk