Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

time calculation incomplete for seconds

Avatar

Level 2

I need the below to include seconds in the output field.  I have looked everywhere and I'm sure simple.  Please help.

if (HasValue(startsamplingtime) and HasValue(endsamplingtime)) then

var TimeDiff = (Time2Num(endsamplingtime.formattedValue, "HH:MM:SS") - Time2Num(startsamplingtime.formattedValue, "HH:MM:SS")) / (1000 * 60 * 60)

//truncate to hours

var HourDiff = Floor(TimeDiff/60)

//get minutes less than 60

var MinDiff = Mod(TimeDiff,60)

need seconds here

var SecDiff =

// build fomatted dispaly string

Concat( Format("Z9", HourDiff), ":", Format("99",MinDiff), ":", Format("99", SecDiff) )

else

// if any values missing null the output

null

endif

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Try something like;

 

if (HasValue(startsamplingtime) and HasValue(endsamplingtime)) then

   var millisecondsPerMinute = 1000 * 60;

   var millisecondsPerHour = millisecondsPerMinute * 60;

   var interval = Time2Num(endsamplingtime.formattedValue, "HH:MM:SS") - Time2Num(startsamplingtime.formattedValue, "HH:MM:SS");

   // Calculate the hours, minutes, and seconds.

   var hours = Floor(interval / millisecondsPerHour );

   interval = interval - (hours * millisecondsPerHour );

   var minutes = Floor(interval / millisecondsPerMinute );

   interval = interval - (minutes * millisecondsPerMinute );

   var seconds = Floor(interval / 1000 );

   Concat( Format("Z9", hours), ":", Format("99",minutes), ":", Format("99", seconds))

endif

Regards

Bruce

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

Hi,

Try something like;

 

if (HasValue(startsamplingtime) and HasValue(endsamplingtime)) then

   var millisecondsPerMinute = 1000 * 60;

   var millisecondsPerHour = millisecondsPerMinute * 60;

   var interval = Time2Num(endsamplingtime.formattedValue, "HH:MM:SS") - Time2Num(startsamplingtime.formattedValue, "HH:MM:SS");

   // Calculate the hours, minutes, and seconds.

   var hours = Floor(interval / millisecondsPerHour );

   interval = interval - (hours * millisecondsPerHour );

   var minutes = Floor(interval / millisecondsPerMinute );

   interval = interval - (minutes * millisecondsPerMinute );

   var seconds = Floor(interval / 1000 );

   Concat( Format("Z9", hours), ":", Format("99",minutes), ":", Format("99", seconds))

endif

Regards

Bruce

Avatar

Level 2

Thanks!  That work perfectly!  My scripting is limited and had already tried multiple codes.

Brandi

Avatar

Level 2

If I want to add in the day for instances that run over would I have to combine date/time field and do calculation or is there a way I could calculate separately and then concatenate?

Thanks