If statement to return result in days or hours
I've created a date calculation (start and end dates as separate fields), but need to return a result of hours / days.
I've got it to display the result, but need it to show whether this is in days or hours. The calculation gives a result in either days or hours, based on how long between start and end, but I need text to show which is being used - for example, 5 hours, or 4 days.
This is my calculation so far:
var end = moment(@End Date of absence);
var start = moment(@Start Date of absence);
var days = end.diff(start, "d");
var hours = end.diff(start, "hours");
var check = days >= 1 ? days : hours;
check
How do I include the word "hours" or "days" in the calculation? I know I'd likely need to change the calculation type to text rather than number, but can I just put
var check = days >= 1 ? days "days" : hours "hrs";
check
Or do I need to put something else?
If possible, I'd really like it to display in hours AND days - for example we have someone who left at 1pm and was off for 2.5 days but it is showing 2 days. However - that's going to be awkward with the difference between working hours and hours in a day.
Thanks in advance - just trying to tidy it up a bit to make it obvious to the person who will be using this data.
-
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");
daysAnd 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).
-
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 : "";
checkThis 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'.
-
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;
daysRainer
Please sign in to leave a comment.
Comments
3 comments