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

calculate difference between to date and convert to time

Avatar

Level 5

Hi All

I try to calculate in FormCalc difference between to dates and display result in time format like HH:MM.

I have two date fields formatted by MM/DD/YYYY: satrtDate and endDate. And TextFeild where I expect to get result.

My code for TextField in calculate event:

differenceD = (Date2Num(endDate, "YYYY-MM-DD") - Date2Num(startDate, "YYYY-MM-DD")) * (1000 * 60 * 60 * 24)

$ = Num2GMTime(differenceD, "HH:MM")

But unfortunately I cannot get the result. What is problem in my calculation? How to fix to calculate correctly?

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Well, as a day only has 24 hours, you cannot use the Num2Time() or Num2GMTime() methods. They can only handle the amount of milliseconds within a 24 hour time frame.

For your scenario you better use a numeric field with the following display pattern:

To calculate the difference use this FormCalc script:

$ = (Date2Num(endDate.formattedValue, "YYYY-MM-DD") - Date2Num(startDate.formattedValue, "YYYY-MM-DD")) * 24

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Well, as a day only has 24 hours, you cannot use the Num2Time() or Num2GMTime() methods. They can only handle the amount of milliseconds within a 24 hour time frame.

For your scenario you better use a numeric field with the following display pattern:

To calculate the difference use this FormCalc script:

$ = (Date2Num(endDate.formattedValue, "YYYY-MM-DD") - Date2Num(startDate.formattedValue, "YYYY-MM-DD")) * 24