-
1. Re: Age field based on date field
GKaiseril Jul 15, 2013 11:46 AM (in response to WillCD)You have to use JavaScript to create the custom calculation script to get the desired result.
For example in decimal years:
function Date2Num(cFormat, cDate) {
/*
convert date string to number of days since Epoch date
Inputs:
cDate - date sting
cFormat - format of date sting
Returns:
number of dasy since Epoc date.
*/
// convert date string to date object
var oDate = util.scand(cFormat, cDate);
// convert milliseconds from Epoch date to whole days
return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
} // end of Date2Numevent.value = "";
// get dob string value
var sDob = this.getField("DOB").value;
if(sDob != "") {
// compute is dob is not empty
var cToday = util.printd( "dd-mmm-yyyy", (new Date()))
// compute difference includig end date
var nDiff = Date2Num(cToday, "dd-mmm-yyyy", cToday) - Date2Num("dd-mmm-yyyy", sDob);
// compute output in years with decimals
event.value =(nDiff / 365.2425) + " years old";
}Years months & days:
event.value = "";
// get dob string value
var sDob = this.getField("DOB").value;
if(sDob != "") {
// convert to date object
var oDob = util.scand("dd-mmm-yyyy", sDob);
// todays date in milliseconds since Epoch date
var oToday = new Date()
// compute output
nYears = oToday.getFullYear() - oDob.getFullYear();
nMonths = oToday.getMonth() - oDob.getMonth();
nDays = oToday.getDate() - oDob.getDate();
if(nDays < 0) {
// table for months days
// fix for days in month
nMonths--;
nDays = nDays + 30;
}
if(nMonths < 0) {
nYears--;
nMonths = nMonths + 12;
}
event.value = nYears + " years " + nMonths + " months " + nDays + " days";
}
Whole years only:
event.value = "";
var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("dd/mm/yyyy", dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
var TodayMMDD = today.getMonth() + today.getDate()
var dobMMDD = dob.getMonth() + dob.getDate();
if( TodayMMDD < dobMMDD) age -= 1;
event.value = age;
}
-
2. Re: Age field based on date field
WillCD Jul 15, 2013 12:32 PM (in response to GKaiseril)This is way over my head. If I copy and paste this into my document (in the properties of the field), will it work?
-
3. Re: Age field based on date field
GKaiseril Jul 15, 2013 12:48 PM (in response to WillCD)Only if your field for he date of birth is named "DOB" in all caps.
-
4. Re: Age field based on date field
WillCD Jul 16, 2013 11:39 AM (in response to GKaiseril)I changed the name of my Text Field (which contains the date of birth) from a102 to DOB. I created a new Text Field called Test and pasted:
event.value = "";
var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("dd/mm/yyyy", dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
var TodayMMDD = today.getMonth() + today.getDate()
var dobMMDD = dob.getMonth() + dob.getDate();
if( TodayMMDD < dobMMDD) age -= 1;
event.value = age;
}
...into the Format tab under Custom and Custom Format Script. Is that right? It does not appear to have worked.
-
5. Re: Age field based on date field
GKaiseril Jul 17, 2013 7:19 AM (in response to WillCD)You are performing a calculation not a formatting action.
You need to put the code in the Custom Calculation Script. You might want to carefully reread the post.
-
6. Re: Age field based on date field
WillCD Jul 17, 2013 8:23 AM (in response to GKaiseril)My mistake. I repasted in Custom Calculation Script and it still did not work. I noticed that in your code, you formatted the date with 2 digits for day, month and 4 digits for year. So I changed my DOB text field to 2 digits for day and month and 4 digits for year. Do those need to be the same?
It still did not work, and then I noticed in your code that you have day first, then month, then year. So I edited the code and put mm/dd/yyy in place of dd/mm/yyyy. I retried and now my new Age text field populates an age when a date of birth is entered into the DOB field. But unfortunately, it was the wrong age! I entered 03/28/1983 into the DOB field and the Age field populated 29, instead of 30. Any thoughts why?
-
7. Re: Age field based on date field
WillCD Jul 17, 2013 9:47 AM (in response to WillCD)Also, I have 6 DOB fields and need 6 age calculations. What part(s) of the calculation code would I need to rename to have 6 different calculations and for the Age fields to correspond to the proper DOB fields? Thanks.
-
8. Re: Age field based on date field
GKaiseril Jul 17, 2013 9:51 AM (in response to WillCD)The comparison for the adjustment of the whole years was going the wrong way.
event.value = "";
var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("dd/mm/yyyy", dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
var TodayMMDD = today.getMonth() + today.getDate()
var dobMMDD = dob.getMonth() + dob.getDate();
if( TodayMMDD > dobMMDD) age -= 1;
event.value = age;
}
-
9. Re: Age field based on date field
WillCD Jul 17, 2013 10:31 AM (in response to GKaiseril)Okay, so I repasted the new code and now some birthdays work and others don't.
Examples:
03/28/1983 correctly shows age 30.
03/24/1984 correctly shows age 29.
08/30/2008 incorrectly shows age 5. Should be 4.
01/16/2011 incorrectly shows age 1. Should be 2.
Thoughts?
-
10. Re: Age field based on date field
WillCD Jul 25, 2013 12:50 PM (in response to WillCD)Does anyone know what I can do here? Does anyone know why the ages are wrong?
-
11. Re: Age field based on date field
Gilad D (try67) Jul 25, 2013 12:55 PM (in response to WillCD)The script assumes the format of the dates is "dd/mm/yyyy", while you're using "mm/dd/yyyy". You need to change one of them...
-
12. Re: Age field based on date field
WillCD Jul 26, 2013 6:10 AM (in response to Gilad D (try67))I believe I have. Here is the script I'm using:
event.value = "";
var dobValue = getField("a104").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
var TodayMMDD = today.getMonth() + today.getDate()
var dobMMDD = dob.getMonth() + dob.getDate();
if( TodayMMDD > dobMMDD) age -= 1;
event.value = age;
}
-
13. Re: Age field based on date field
WillCD Jul 26, 2013 6:16 AM (in response to WillCD)The format of the text field that this is pulling from is mm/dd/yyyy.
I just checked and all seem to be a year off now.
03/28/1983 showing 29, instead of 30. (This is 1 year too YOUNG.)
03/24/1984 showing 28, instead of 29. (This is 1 year too YOUNG.)
08/30/2008 showing 5, instead of 4. (This is 1 year too OLD.)
01/16/2011 showing 1, instead of 2. (This is 1 year too YOUNG.)
Any ideas why it's always off 1 year and why sometimes it's 1 year too old and sometimes it's 1 year too young?
-
14. Re: Age field based on date field
Gilad D (try67) Jul 26, 2013 7:08 AM (in response to WillCD)The script you were given is invalid. Use this instead:
event.value = "";
var dobValue = getField("a104").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
var age = today.getFullYear() - dob.getFullYear() - 1;
if (today.getMonth()>dob.getMonth()) {
age++;
} else if (today.getMonth()==dob.getMonth()) {
if (today.getDate()>dob.getDate())
age++;
}
event.value = age;
}
-
15. Re: Age field based on date field
WillCD Jul 26, 2013 7:55 AM (in response to Gilad D (try67))This works. Thank you. I did find one problem which is not the end of the world. I put in 07/26/XXXX and it is 1 year too young. (That is today's date.) Is it one day behind when it comes to calculating on the same day as the birth day?
-
16. Re: Age field based on date field
Gilad D (try67) Jul 26, 2013 8:00 AM (in response to WillCD)It's just a question of when to make the change from one year to another. If you want to include the birthday itself in the next year, change this line in the code:
if (today.getDate()>dob.getDate())
To this:
if (today.getDate()>=dob.getDate())
-
17. Re: Age field based on date field
GKaiseril Jul 26, 2013 9:53 AM (in response to WillCD)Here is a working file for computing the Age from the DOB.
Note the formatting for the DOB field must be the same for the DOB field and the script.
Formatting of the entry is very important.
event.value = "";
// name for DOB field
DOB_field_name = "DOB";
// format for dob field
var cDateFormat = "dd-mmm-yyyy";
try {
var dobValue = getField(DOB_field_name).value;
} catch (e) {
app.alert("Error getting value of field " + DOB_field_name, 0, 1);
dobValue = "";
} finally {
if (dobValue!="") {
var dob = util.scand(cDateFormat, dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
// adjust for months after month of birth
if (today.getMonth() < dob.getMonth()) age--;
// adjust for today month equal to dob month and today's date after day of birth
if ((today.getMonth() == dob.getMonth()) && (today.getDate() < dob.getDate())) age--;
event.value = age;
} // end dobValue not empty
} // end get field value
-
18. Re: Age field based on date field
WillCD Jul 26, 2013 10:06 AM (in response to Gilad D (try67))This fixed the birthday issue. Thank you.
-
19. Re: Age field based on date field
WillCD Oct 15, 2014 11:10 AM (in response to Gilad D (try67))Hey Gilad D, I noticed that this no longer works on the pdf once they become older. It's as if it is calculating the field once a birthday is originally entered and then never calculates it again. Is there a way to have this be dynamic, so that when I open the pdf, it will show the correct age presently?
-
20. Re: Age field based on date field
Gilad D (try67) Oct 15, 2014 11:14 AM (in response to WillCD)Yes. You need to embed the code at the document level, so that it gets executed each time the file is opened. Basically the only thing you need to change in it is instead of accessing event.value you need to use this.getField("Age").value, or whatever is the name of the Age field in your file.
-
21. Re: Age field based on date field
WillCD Oct 15, 2014 11:23 AM (in response to Gilad D (try67))Here is the code I currently have. What would you change?
event.value = "";
var dobValue = getField("a104").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
var age = today.getFullYear() - dob.getFullYear() - 1;
if (today.getMonth()>dob.getMonth()) {
age++;
} else if (today.getMonth()==dob.getMonth()) {
if (today.getDate()>=dob.getDate())
age++;
}
event.value = age;
}
-
22. Re: Age field based on date field
Gilad D (try67) Oct 15, 2014 11:26 AM (in response to WillCD)Exactly what I've described.
-
23. Re: Age field based on date field
GKaiseril Oct 15, 2014 5:00 PM (in response to WillCD)Have you checked the JavaScript Console for errors?
Can you post a link to a sample form?
The form I linked to works and adjusts with each new entry.
-
24. Re: Age field based on date field
WillCD Oct 16, 2014 10:43 AM (in response to Gilad D (try67))Gilad D, here is what I have in the field. Does this look right? My Date of Birth field is a104 and my age field is a102. This does not seem to work. Yesterday (10/15/2014), I put the Date of Birth as 10/16/2013 and the age field populated a "0." That is correct. When I opened it up today, the age field should have populated a "1," but it did not. This is the problem I'm running into. Thoughts?
this.getField("a102").value = "";
var dobValue = getField("a104").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
var age = today.getFullYear() - dob.getFullYear() - 1;
if (today.getMonth()>dob.getMonth()) {
age++;
} else if (today.getMonth()==dob.getMonth()) {
if (today.getDate()>=dob.getDate())
age++;
}
this.getField("a102").value = age;
}
-
25. Re: Age field based on date field
WillCD Oct 16, 2014 10:45 AM (in response to GKaiseril)GKaiseril, I want to make sure you understand my dilemma. I need the age field to be dynamic and not static. The age field correctly populates when I enter in the Date of Birth, but the age field never changes, even when a year passes by and the age is now older. Does that make sense?
Yesterday, I did a test and put in a DOB as 10/16/2013, which would mean the age yesterday was "0" and the age today would be "1." The pdf still says "0" today. Thoughts?
-
26. Re: Age field based on date field
GKaiseril Oct 16, 2014 11:11 AM (in response to WillCD)Again, are you getting any errors in the JavaScript console?
Again can you post a link to the form?
I would set the default value of the DOB field to the value of the field and then create a document level script to reset the DOB field.
-
27. Re: Age field based on date field
WillCD Oct 16, 2014 11:56 AM (in response to GKaiseril)GKaiseril, I'm not sure what the JavaScript console is. I can't post a link to the form unfortunately. I wish I could. Here's what happens:
I set the DOB and the age is correct. Once the next DOB happens, the age stays the same. It does not increase by a year, as it should each time the DOB is reached on the calendar. But if I delete the DOB and re-enter the same DOB, the age updates to the correct age. This is what happened with the old code and the new one from Gilad D. What happens on your end when you enter the DOB as tomorrow? Does the age update itself the next day?
-
28. Re: Age field based on date field
GKaiseril Oct 16, 2014 12:02 PM (in response to WillCD)
If you force the PDF to recalculate by changing a field or resetting a field used in a calculation, then all the scripts in the calculation action will be recalculated.Since you know so little, others need to see the form and the calculations.
-
-
30. Re: Age field based on date field
WillCD Oct 28, 2014 6:11 AM (in response to WillCD)So, did you guys see the form?
-
31. Re: Age field based on date field
Gilad D (try67) Oct 28, 2014 6:14 AM (in response to WillCD)I don't think anyone is going to install an unknown browser plugin in order to help you out. If you want help provide a direct link to the file.
-
32. Re: Age field based on date field
WillCD Oct 28, 2014 6:21 AM (in response to Gilad D (try67))Unknown? I thought Transporter was well-known. How do I provide a direct link to the file?
-
33. Re: Age field based on date field
Gilad D (try67) Oct 28, 2014 6:24 AM (in response to WillCD)I've never heard of it... Upload the file to acrobat.com or dropbox or mega or any of the other free and easy-to-use file-sharing web-sites and post the sharing link to it here.
-
-
35. Re: Age field based on date field
Gilad D (try67) Oct 28, 2014 6:52 AM (in response to WillCD)Replace in the code you got the instances of this.getField("a102").value with event.value and it works just fine...
-
36. Re: Age field based on date field
WillCD Oct 29, 2014 10:57 AM (in response to Gilad D (try67))Gilad D, are you understanding the problem? The problem is getting the field to update when a birthday happens. I changed the code like you said and it did not work. I entered a DOB of 10/29/2013 on 10/28/2014. The age correctly populated as 0. But today, it should read 1 and it still reads 0. Thoughts?
-
37. Re: Age field based on date field
GKaiseril Oct 29, 2014 11:57 AM (in response to WillCD)I would use hidden form field that contains today's date and update that field when the form opens and that would force the calculation of the age. I would also use that hidden form field in the calculation for the age.
-
38. Re: Age field based on date field
WillCD Oct 30, 2014 12:03 PM (in response to GKaiseril)Thanks. I would not know how to do that. What is the form using to calculate the date when the DOB is first entered? Wouldn't that value be dynamic? Shouldn't this work without having to make a hidden form field?
-
39. Re: Age field based on date field
Gilad D (try67) Oct 30, 2014 12:09 PM (in response to WillCD)You can add a special condition to the code that if the date and month are
the same it should add one more year to the age. No need for extra fields.



