If statement to return result in days or hours

Comments

3 comments

  • Susan Warren-Moy

    I have edited this so that the days and hours are now separate fields, this is going to be easier to read for the person who needs it.

    However, I've noticed a problem - the code isn't picking up days fully - e.g. we have someone who has logged sickness as 27th June 09:00 start, 1st July 17:00 end. This should show as 5 days but is showing as 4.

    The code I have for the Days field is this:

    var end = moment(@End Date of absence);
    var start = moment(@Start Date of absence);
    var days = end.diff(start, "d");
    days

    And the app is displaying this:

    Please can someone help me work out how to pick up all the days even if it's not 24 hours? (this would also help when someone is off sick for one day, it's showing as 0 as it's less than 24 hours).

    0
    Comment actions Permalink
  • Susan Warren-Moy

    I've done some more digging and found that I can get the days to 2 decimal points, but I'm now also seeing that for example, if someone is absent from 10th June to 15th June, it shows as 5 days, not 6.

    If they put in a start and end time, I might see e.g. 5.75 days, but if they do not put start and end times - because they were off for a full day - it miscounts by 1 day.

    var end = moment(@End Date of absence);
    var start = moment(@Start Date of absence);
    var days = end.diff(start, "d", true);
    var check = days >= 1 ? days : "";
    check

    This code is what I'm using, I know there must be a way to set this so that it can count the right number of days, I'm just scratching my head as to how to do this.

    The hours field works fine, as it will only show for entries which are under 24 hours, this is also set to show "true" so that it picks up e.g.  7.5 hours rather than rounding up or down. (exactly the same coding other than using 'hours' instead of 'days'.

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Susann,
    for full days it counts "wrong" because in dates w/o a time the time is automatically set to 00:00,
    So if start = Monday 00:00 and end = Wednesday 00:00 the diff is really 2 days (someone is absent the whole Monday, the whole Tuesday but back on Wednesday 00:00).  Means: You must check the time in start and end and if it is in both 00:00 you must add 1 day, if not (= in min. 1 date is a dedicated time entered) add 0 days.

    var end = moment(@End Date of absence);
    var start = moment(@Start Date of absence);
    var endHour =  end.format("HHmm");
    var startHour = start.format("HHmm");
    var dayToAdd = endHour == "0000" && startHour == "0000" ? 1 : 0;
    var days = end.diff(start, "d", true) + dayToAdd;
    days

    Rainer

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk