Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Aug 25, 2016 Aug 25, 2016

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.

TOPICS
Acrobat SDK and JavaScript
902
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 25, 2016 Aug 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;

Translate
Community Expert ,
Aug 25, 2016 Aug 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2016 Aug 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());

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2016 Aug 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2016 Aug 25, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2016 Aug 25, 2016
LATEST

No need to... It's quite a tricky thing to get. Took me a couple of moments

to figure out what was going wrong.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines