Highlighted

problem with date

ibwaheemi

27-11-2008

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.

Replies

Highlighted

pguerett

28-11-2008

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
Highlighted
Highlighted

_James_J_Gallag

15-12-2008

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()
Highlighted
Highlighted

pguerett

15-12-2008

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.