Expand my Community achievements bar.

Third Time Lucky to get Age from DOB entry

Avatar

Former Community Member
Not exactly sure why I haven't had a response because through weeks of looking through the Forum, I cannot find how to do this.



I currently have the CurrentDate field, I wish to obtain a persons age only after the entry of a persons DOB. This DOB could be either entered in a DateField or three separate NumericalField's, therefore the year could be subtracted from the YYYY of the CurrentDate field. The only addition is that it updates upon viewing (ie when a Birthdate has passed the age would be recalculated)



I dont mind using FormCalc or JavaScript, which ever one gets the right result.



Could someone please respond, its the last thing (at this point in time) that I need to do for my form, I cant submit it otherwise.



Thankyou In Advance

Jason
12 Replies

Avatar

Former Community Member
lol, I think that no one is responding to your posts because they don't want to do your work for you.



Here's what you do.



Subtract the current date from the date entered. Adjust the result depending on if the date has passed in the current year.



JavaScript help can be found at www.w3schools.com. I'm not sure where you get FormCalc help, as I tend not to use it.



You can probably find a fully written function in a JavaScript forum somewhere. Best place to start for that is Google.



Don't forget to recalculate the age when the form opens so it will update the age when it is viewed. The Form:ready event would probably be the best spot.



Cheers,

Stone

Avatar

Former Community Member
Thanks Stone,



I understand that people would not want to do my work for me, but i really dont know where to start. I'm surprised that no one has asked the question before, I didn't think it would be this hard.



Thanks for the tips, I'm looking through the site now, I have looked on Google before to get a script, but that hasn't been to fruitful.



Ill keep trying, unless someone is able to assist with more detail.



Thanks again

Jason

Avatar

Level 6
Hi Jason,



Here's one way, which works for me. It might not be the most straight forward way, but it makes sense. In this case, the date is in a field called DateOfBirth in the format MM/DD/YYYY. Hope it helps.



Jared Langdon

J. Langdon Consulting

www.jlangdon.ca



var strDOBYear = DateOfBirth.rawValue.substr(6,4);

var strDOBMonth = DateOfBirth.rawValue.substr(0,2);

var strDOBDay = DateOfBirth.rawValue.substr(3,2);

var oDOB = new Date(parseInt(strDOBYear,10), parseInt(strDOBMonth,10)-1, parseInt(strDOBDay,10) );

var oToday = new Date();



//determine whether the person's birthday has already occurred this year

var boolBirthdayHasOccurred = false;

if (oDOB.getMonth() < oToday.getMonth())

boolBirthdayHasOccurred = true;

if ((oDOB.getMonth() == oToday.getMonth()) && (oDOB.getDate() <= oToday.getDate()))

boolBirthdayHasOccurred = true;



var nCurrentAge = 0;

if (boolBirthdayHasOccurred)

nCurrentAge = oToday.getFullYear() - oDOB.getFullYear();

else

nCurrentAge = oToday.getFullYear() - oDOB.getFullYear() - 1;

Avatar

Former Community Member
Mate,



Your a genius, that has saved a lot of time, it works well, accept is there any way of changing the format to DD/MM/YYYY. Picky I know, just hoping that it is possible. Otherwise brilliant.



Thankyou

Jason

Avatar

Level 6
Glad to help. If your date is in the DD/MM/YYYY format, then the first three lines should be

var strDOBYear = DateOfBirth.rawValue.substr(6,4);

var strDOBMonth = DateOfBirth.rawValue.substr(3,2);

var strDOBDay = DateOfBirth.rawValue.substr(0,2);



Careful though, this will work for 20/01/2006 but not with 20/1/2006. If your dates are like the latter, then another approach would be to split the string into an array, using the '/' as a delimiter. I did not test this code.



var strDate = "20/1/2006";

var arrDate = strDate.split("/");

var strDay = arrDate[0]; //20

var strMonth = arrDate[1]; //1

var strYear = arrDate[2]; //2006

Avatar

Former Community Member
Hi,



I´m having this little problem... I don´t know how to subtract dates in JS. My situation is this: there are two fields, one is a date field and the other a regular text field. Your suppose to fill in the date field and after that the text field with how many days do you wish to send a message before the limit(first field). The message part I already know how to do(not in JavaScript), I only need the second part. Could someone please help...

Avatar

Level 6
This code creates a date object called oDate2 that is 300 days after oDate1.



var oDate2 = new Date(oDate1.getFullYear(),oDate1.getMonth(),oDate1.getDate()+300);



Hope this helps,



Jared Langdon

http://www.jlangdon.ca

Avatar

Former Community Member
I'm trying to add 364 days to a year using formcalc but just cant figure it out?



Anyone know the best way to do this.



Thanks!

Avatar

Level 7
The code is JavaScript not FormCalc, so you should be using JavaScript for the language.

Avatar

Former Community Member
Does the code go into the calculation field (age field)? If my binding DOB field name is "birthdate" then where is that referenced in the script?

Avatar

Former Community Member
Used Jared Langdon javascript. It worked perfectly after some tweeking on my end. Just need to remove calender drop down menu function from Date field. Calendar feature does not agree with script. Will try to use text field in its place using same date display and edit patterns. Last hurtle I'm facing is creating an input mask function you would see in MS Access.

Avatar

Former Community Member
Hi,



I'm trying to also calculate age from DOB using the script above. Can you please tell me how I can inactivate the calendar drop-down function from the Date field?



Thanks!

Annie