Expand my Community achievements bar.

AEM 6.5 HTML5 Form - Calculate days between two dates

Avatar

Level 5

Hi,

I am trying to calculate the difference between two dates in an HTML5 form. The following code in the XDP template/form works in Designer's PDF preview tab but it does not work in Designer's "Preview HTML' tab and when the form is uploaded in AEM and rendered as HTML form. The field appears blank.

The calculate event with FormCalc Language is used.

Abs(Date2Num(Table1.Row3.DateField2, "YYYY-MM-DD") - Date2Num(Table1.Row2.DateField1, "YYYY-MM-DD"))

I also tried the DateDiff function as JavaScript language and the result was also blank. (Ref: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-by-category/date-an...)

DateDiff("d", Table1.Row2.DateField2, Table1.Row3.DateField1)

What date functions are supported for HTML forms in AEM 6.5? Is there a sample script that works for HTML forms.

Thanks,

Leena

1 Reply

Avatar

Level 5

In case this helps anyone else, I converted the date field to date object and then calculated the days. The 86400000 refers to milliseconds. This uses the calculate event and javascript language. This works in Designer's 'Preview HTML'  and for AEM HTML5 forms.

// Convert date field to date object
const date1 = new Date(form1.Page1.Table1.Row2.dt_1.rawValue);
const date2 = new Date(form1.Page1.Table1.Row3.dt_2.rawValue);
const days = ((Math.abs(date2 - date1))/86400000)
this.rawValue=days

~Leena