Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

problem with date

Avatar

Level 2
i am trying to display an error message if a customer is over 18



----- topmostSubform.Page1.dob::exit: - (FormCalc, client) ---------------

//todays date

var today = num2date(date(), DateFmt(1))



//difference between today and date of birth 'dob'

var diff = today - Date2Num(dob.rawValue, "DD/MM/YYYY")



//is it over 18 years 18*365.25=6574.5

if (diff > 6574.5) then



//alert message

xfa.host.messageBox(Check the customers age they may be over 18!)

endif



--------------------------------------------------------------------------



a textbox that displays the age would be enough but i dont think that is possible.
5 Replies

Avatar

Former Community Member
Yes it is possible ...the date() function returns the number of days since the epoch so you do not need to wrap it in a num2date call. It should be:



var today = date()



I also do not think you shodul name your variable today. Change its name to something else. Here is the code that I used:



//todays date

var num_today = date()



//difference between today and date of birth 'dob'



//xfa.host.messageBox(dob.rawValue)

var diff = num_today - Date2Num(dob.rawValue, "YYYY-MM-DD")



//is it over 18 years 18*365.25=6574.5

if (diff > 6574.5) then

xfa.host.messageBox("Check the age ...you may be over 18!")

endif

Avatar

Level 2
Paul your improved code worked a treat



again youved help me ;)



you are a diamond in a heap of stones



thank you

Avatar

Level 3
I am trying to do something similar to the above in the exit event of a field:



if (this.rawValue != "")

{

var num_today = Date();

var diff = num_today - date2Num(this.rawValue);

xfa.host.messageBox(diff);

}



I am just outputting the difference to a message box just to view it myself but unfortunatlyu I'm experiencing a problem before I get that far.



When I exit the field I get the following action script error:



script execution error : MemberForm[0].#subform[4].subDependantList[0].subDependant[0].dob[0]:exit

ReferenceError: Error #1065: Variable Date2Num is not defined.

at xfamx.scripting::Node/execEvent()

at ga.model::PanelItem/focusOutHandler()

at flash.display::Stage/set focus()

at mx.core::UIComponent/setFocus()

at mx.managers::FocusManager/setFocus()

at mx.managers::FocusManager/mouseDownHandler()

Avatar

Level 3
Ok I see that the Date2Num() function in only for formcalc. Is there a similar function in Javascript ??

Avatar

Former Community Member
There is a function that returns the # of milliseconds from the epoch but you have to create a date object first. Do a google search of date arithmetic for javascript and you will see plenty of examples.