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.

Javascript that calculates the difference between two times

Avatar

Former Community Member

Hi all..

I can't calculate the difference between two times fields.

For example: I have 3 dateTime fiels but in the Date Format I need only the time (hour)

How can I calculate the difference between 2 times?

time 1:  15:45:00

time2:   16:30:00

The result is: ....00:45:00

Thanks a Lot

María

3 Replies

Avatar

Level 10

Hi,

this can be done easy with FormCalc.

Put this script into the calculate:Event of the field which should display the difference.

Avatar

Level 2

I have tried to get this code to work but without any luck - I am not sure what I am doing wrong - I have ensured the two fields are called tstart and tend and am using formcalc and calculate - I tried adjusting the code slightly but still without any luck.

Please can you help?

I am trying achieve how much time between a start time and end time (ideally a code that works overnight) - I am using Livecycle ES2

var tStart = Time2Num(tstart.formattedValue, "HH:MM:SS")

var tEnd = Time2Num(tend.formattedValue, "HH:MM:SS")

if (tStart ne 0 and tEnd ne 0) then

     //if start time is lower than end time.

     if (tStart < tEnd) then

          Num2Time(Abs(tStart - tEnd) - 7200000 + 1000, "HH:MM:SS")

     //If start time is higher than end time    

     else

          Num2Time(Abs(86400000 - Abs(tStart - tEnd) - 7200000 + 1000), "HH:MM:SS")

     endif

endif