Age calculation

Hello,

In FM8 this calculation would work just fine to figure out a person’s age given today’s date (Date) and the person’s birth date (DOB):
=Year (Date) - Year (DOB) - ((Month(Date) + Day (Date)/100) < (Month(DOB) + (DOB)/100)).

How would a method in Servoy look like?

Thanks,

create a global script named “calculateAge” (you can copy paste this code)
This global script receives 3 args from the calling script
that may be anywhere in your solution.
NOTE: please check if I’ve translated your formula correctly.
My FM knowledge is getting a bit rosty :wink:

var birthdayDateObject = new Date(arguments[0],arguments[1], arguments[2])
var todayDateObject = new Date() //today dateObject

var yearResult = todayDateObject.getFullYear() - birthdayDateObject.getFullYear() 
var a = todayDateObject.getMonth() + (todayDateObject.getDate()/100)
var b = birthdayDateObject.getMonth() + (birthdayDateObject.getDate()/100)
var c = 0
if(a<b)
{
	c=1
}

return yearResult - c //return this value to the calling script

the calling script looks like

var age = globals.calculateAge(1966,10,30); //!! month is zero based >> Jan=0 Feb=1
application.output(age) //for testing. Shows result in output tab of editor (bottom left)

Or use the tools plugin http://www.it2be.com/plugins.htm#tools

var daysAfterBirth = plugins.it2be_tools.dateDifference(new Date(), new Date(1963, 05, 26), 5)

application.output(daysAfterBirth)

Be careful, in this example month 05 is June.

Have fun