Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

how to roundup time calculation to nearest hour?

Avatar

Level 1

Hi,

I'm trying to round up a calculation result to the nearest hours

for example, I want the result: 4:49 to be 5

I have the a start time field and an endtime field and the a totalhours field which I have managed to get to work using the following script:

var startT = StartTime.formattedValue

var endT = EndTime.formattedValue

var differenceT = Time2Num(endT, "HH:MM") - Time2Num(startT, "HH:MM")

$ = Num2GMTime(differenceT, "H:MM")

However, the output is coming up as:

ss.jpg

4:59, when I want it to just say 5

Any help here would be greatly appreciated.

Please note, I have a very basic knowledge of this scripting just by searching the web, so baby steps would be really appreciated. Thank you :-)

1 Reply

Avatar

Level 10

Hi,

I think you calculation should be;


var differenceT = Floor(( Time2Num(TimeField5.formattedValue, "HH:MM") - Time2Num(TimeField4.formattedValue, "HH:MM") + (60 * 60 * 1000) - 1 ) / (60 * 60 * 1000)) * (60 * 60 * 1000)+1


That is add an hour minus one millisecond, truncate to get the hours, then multiple by hours to get back to something for Num2GMTime.


Regards


Bruce