Skip to main content
Participant
August 25, 2016
Answered

How could I use a date formatted as 'yyyymmdd' to calculate an age?

  • August 25, 2016
  • 1 reply
  • 991 views

I am making a form and I want to use a military formatted date 'yyyymmdd' to calculate the user's age. I have found code to find the age based off of a date formatted as 'yyyy/mm/dd', but I'm having problems finding the age after adding the slashes with javascript after inputting the date. I'm using Adobe Acrobat Pro DC on OSX 10.11.6. Thanks in advance.

This topic has been closed for replies.
Correct answer try67

Replace these two lines:

var cDob = getField(cBirthDate).value;

var cCutoff = getField(cCutoffDate).value;

With this:

var cDob = getField(cBirthDate).valueAsString;

var cCutoff = getField(cCutoffDate).valueAsString;

1 reply

try67
Community Expert
Community Expert
August 25, 2016

You don't need to add slashes. Just change the code so that it uses the date format you're using.
If you don't know how to do that post the code and we'll help you out.

SpadeEXEAuthor
Participant
August 25, 2016

Here is some code I found on another question, and like I said, it works with the dates having the slashes, but throws errors when they don't even with the format as 'yyyymmdd'

function GetField(cName) {

var oField = this.getField(cName);

if (oField == null) {

app.alert("Field " + cName + " not found.\nPlease check spelling and capitalization of the field name.", 0, 1);

}

return oField;

}

function Scand(cFormat, cDate) {

var oDate = util.scand(cFormat, cDate);

if(oDate ==  null) {

app.alert("Error converting " + cDate + " with format " + cFormat + "\nConversion failed.", 0, 1)

}

return oDate;

}

var cBirthDate = "App.DOB";

var cCutoffDate = "Form.Date";

var cDateFormat = "yyyymmdd";

event.value = "";

var cDob = getField(cBirthDate).value;

var cCutoff = getField(cCutoffDate).value;

if (cDob != "" && cCutoff != "") {

var oDob = Scand(cDateFormat, cDob);

var oCutoff = Scand(cDateFormat, cCutoff);

event.value = oCutoff.getFullYear() - oDob.getFullYear();

event.value = event.value - Number(oCutoff.getMonth() < oDob.getMonth());

event.value -= Number((oCutoff.getMonth() == oDob.getMonth())) && Number(oCutoff.getDate() < oDob.getDate());

}

SpadeEXEAuthor
Participant
August 25, 2016

Replace these two lines:

var cDob = getField(cBirthDate).value;

var cCutoff = getField(cCutoffDate).value;

With this:

var cDob = getField(cBirthDate).valueAsString;

var cCutoff = getField(cCutoffDate).valueAsString;


I could have sworn I did that... I feel like a complete idiot. Thanks again.