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.

Calculating the time difference between to fields

Avatar

Level 3

I have 3 fields;

Time Field 1 called StartTime

Time Field 2 called EndTime

I'd like to calculate the difference between these 2 fields into the another field called TotalTime in hours.

EndTime cannot be greater or equal to StartTime. In this case a message should warn the user.

Could you help me with that please?

4 Replies

Avatar

Level 7

You would use the functions Time2Num and Num2GMTime to do this. In the calculate event of the total time field you would put something like (in formcalc):

var startT = StartTime.formattedValue

var endT = EndTime.formattedValue

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

$ = Num2GMTime(differenceT, "HH:MM:SS")

- the "HH:MM:SS" bit should be whatever time pattern you have used.

Avatar

Level 3

Thank you it's calculating but not as I was expecting. Basically total hours needs to be multiplied with a "$" rate in the total text field but your script only puts a time again so result needs to be a number instead of a time!. Also EndTime cannot be greater or equal to StartTime. In this case a message should warn the user. Is it possible to achieve this?

Kind regards

Avatar

Level 7

The result should be the number of hours/minutes difference - but yes with that pattern it shows as a time.

To make sure the end time is after the start time use Time2Num for both and then make sure the number of the end time is larger than the number of the start time.

Avatar

Level 2

I am not sure what I am doing wrong - but using this script I get an answer of zero?

startT = StartTime.formattedValue

endT = EndTime.formattedValue

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

= Num2GMTime(differenceT, "HH:MM:SS")

For the answer field I use a numericfield

Can you help please?