Hour Calculation between days


I need help creating a logic for calculating total number of hours.
Once the person selects the start date, start time, end date and end time the total hour should be displayed.
Can someone help me with creating the logic please.
timecalculation.rp (45.5 KB)

In short, you need to do two things: get the two pairs of dates/times in a form you can process, and then convert that back into hours.

To do this we first parse the input from the fields using various string manipulation methods to get the pieces we need (year, month, date, hour, minutes - seconds and milliseconds are assumed to be 0) then we use the Date.UTC() method to convert that into a valid UTC timestring and call the valueOf() method on that to convert it into unix epoch milliseconds. Then all we have to do is subtract that first date from the second to get the difference in milliseconds, and divide that a few times to convert it back to hours (milliseconds / 1000 / 60 / 60 = hours.

The date fields in this example file assume day month year (separated by spaces) but you can change this by changing how you parse it. The time fields grab the first two characters for hour (so use a leading 0) and the last two for minute. Again, you can make this more robust if you like, I kept it basic for this example.
Date.UTC(year, month, date, hour, minute, seconds, millisecond)

hours-between.rp (48.3 KB)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.