Expand my Community achievements bar.

Calculating age to a specific date

Avatar

Level 1

I have a file that calculates someones age to the current date.  In one field you type the date, and in the other the age appears.  I need it to calculate a persons age at October 1, 2010 instead of the current date.  I do not know how to attach the file so here is the xml code as it appears in LiveCycle

<?xml version="1.0" encoding="UTF-8"?>

<?xfa generator="AdobeLiveCycleDesignerES_V9.0.0.0.20091029.1.612548" APIVersion="3.1.9277.0"?>

<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2010-07-18T20:08:58Z" uuid="76c87f42-7062-4fca-a1ed-205e34ae0e47">

<template xmlns="http://www.xfa.org/schema/xfa-template/2.6/">

   <?formServer defaultPDFRenderFormat acrobat8.1static?>

   <subform name="form1" layout="tb" locale="en_US" restoreState="auto">

      <pageSet>

         <pageArea name="Page1" id="Page1">

            <contentArea x="0.25in" y="0.25in" w="8in" h="10.5in"/>

            <medium stock="letter" short="8.5in" long="11in"/>

            <?templateDesigner expand 1?></pageArea>

         <?templateDesigner expand 1?></pageSet>

      <subform w="8in" h="10.5in">

         <field name="DOB" y="22.225mm" x="25.4mm" w="62mm" h="9mm">

            <ui>

               <textEdit multiLine="1">

                  <border presence="hidden">

                     <?templateDesigner StyleID aped0?></border>

                  <margin/>

               </textEdit>

            </ui>

            <font typeface="Myriad Pro"/>

            <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>

            <para vAlign="middle"/>

            <caption reserve="20.7516mm">

               <para vAlign="middle"/>

               <value>

                  <text>Date of Birth:</text>

               </value>

            </caption>

         </field>

         <field name="Age" y="22.225mm" x="101.6mm" w="25.4mm" h="9mm">

            <ui>

               <numericEdit>

                  <border>

                     <?templateDesigner StyleID aped3?>

                     <edge stroke="lowered"/>

                  </border>

                  <margin/>

               </numericEdit>

            </ui>

            <font typeface="Myriad Pro"/>

            <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>

            <para vAlign="middle"/>

            <caption reserve="8.0666mm">

               <para vAlign="middle"/>

               <value>

                  <text>Age</text>

               </value>

            </caption>

            <calculate override="warning">

               <script contentType="application/x-javascript">

var ageInYears = 0;

var birthDate = new Date();

if ( DOB.rawValue != null )

{

// Get current date, and convert user-entered date to a Date object.

var curDate = new Date();

birthDate.setTime(Date.parse(DOB.rawValue));

// Set initial age, current year - birth year

ageInYears = curDate.getFullYear() - birthDate.getFullYear();

// If we haven't reached the birth month yet, not yet 1 year older...

if ( curDate.getMonth() &lt; birthDate.getMonth() )

ageInYears--;

// If this month is same as birth month but we haven't reached the

// birth date yet, not yet 1 year older...

if ( curDate.getMonth() == birthDate.getMonth() )

if ( curDate.getDate() &lt; birthDate.getDate() )

ageInYears--;

// Set age in result field.

this.rawValue = ageInYears;

}

</script>

            </calculate>

         </field>

         <?templateDesigner expand 1?></subform>

      <proto/>

      <desc>

         <text name="version">8.2.1.3158.1.475346.466429</text>

      </desc>

      <?templateDesigner expand 1?></subform>

   <?templateDesigner DefaultLanguage FormCalc?>

   <?templateDesigner DefaultRunAt client?>

   <?templateDesigner Grid show:1, snap:1, units:0, color:ff8080, origin:(0,0), interval:(125000,125000)?>

   <?acrobat JavaScript strictScoping?>

   <?templateDesigner Zoom 89?>

   <?templateDesigner FormTargetVersion 26?>

   <?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?>

   <?templateDesigner SaveTaggedPDF 1?>

   <?templateDesigner SavePDFWithEmbeddedFonts 1?></template>

<config xmlns="http://www.xfa.org/schema/xci/2.6/">

   <agent name="designer">

      <!--  [0..n]  -->

      <destination>pdf</destination>

      <pdf>

         <!--  [0..n]  -->

         <fontInfo/>

      </pdf>

   </agent>

   <present>

      <!--  [0..n]  -->

      <pdf>

         <!--  [0..n]  -->

         <version>1.7</version>

         <adobeExtensionLevel>1</adobeExtensionLevel>

      </pdf>

      <common/>

      <script>

         <runScripts>server</runScripts>

      </script>

      <xdp>

         <packets>*</packets>

      </xdp>

   </present>

</config>

<localeSet xmlns="http://www.xfa.org/schema/xfa-locale-set/2.6/">

   <locale name="en_US" desc="English (United States)">

      <calendarSymbols name="gregorian">

         <monthNames>

            <month>January</month>

            <month>February</month>

            <month>March</month>

            <month>April</month>

            <month>May</month>

            <month>June</month>

            <month>July</month>

            <month>August</month>

            <month>September</month>

            <month>October</month>

            <month>November</month>

            <month>December</month>

         </monthNames>

         <monthNames abbr="1">

            <month>Jan</month>

            <month>Feb</month>

            <month>Mar</month>

            <month>Apr</month>

            <month>May</month>

            <month>Jun</month>

            <month>Jul</month>

            <month>Aug</month>

            <month>Sep</month>

            <month>Oct</month>

            <month>Nov</month>

            <month>Dec</month>

         </monthNames>

         <dayNames>

            <day>Sunday</day>

            <day>Monday</day>

            <day>Tuesday</day>

            <day>Wednesday</day>

            <day>Thursday</day>

            <day>Friday</day>

            <day>Saturday</day>

         </dayNames>

         <dayNames abbr="1">

            <day>Sun</day>

            <day>Mon</day>

            <day>Tue</day>

            <day>Wed</day>

            <day>Thu</day>

            <day>Fri</day>

            <day>Sat</day>

         </dayNames>

         <meridiemNames>

            <meridiem>AM</meridiem>

            <meridiem>PM</meridiem>

         </meridiemNames>

         <eraNames>

            <era>BC</era>

            <era>AD</era>

         </eraNames>

      </calendarSymbols>

      <datePatterns>

         <datePattern name="full">EEEE, MMMM D, YYYY</datePattern>

         <datePattern name="long">MMMM D, YYYY</datePattern>

         <datePattern name="med">MMM D, YYYY</datePattern>

         <datePattern name="short">M/D/YY</datePattern>

      </datePatterns>

      <timePatterns>

         <timePattern name="full">h:MM:SS A Z</timePattern>

         <timePattern name="long">h:MM:SS A Z</timePattern>

         <timePattern name="med">h:MM:SS A</timePattern>

         <timePattern name="short">h:MM A</timePattern>

      </timePatterns>

      <dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>

      <numberPatterns>

         <numberPattern name="numeric">z,zz9.zzz</numberPattern>

         <numberPattern name="currency">$z,zz9.99|($z,zz9.99)</numberPattern>

         <numberPattern name="percent">z,zz9%</numberPattern>

      </numberPatterns>

      <numberSymbols>

         <numberSymbol name="decimal">.</numberSymbol>

         <numberSymbol name="grouping">,</numberSymbol>

         <numberSymbol name="percent">%</numberSymbol>

         <numberSymbol name="minus">-</numberSymbol>

         <numberSymbol name="zero">0</numberSymbol>

      </numberSymbols>

      <currencySymbols>

         <currencySymbol name="symbol">$</currencySymbol>

         <currencySymbol name="isoname">USD</currencySymbol>

         <currencySymbol name="decimal">.</currencySymbol>

      </currencySymbols>

      <typefaces>

         <typeface name="Myriad Pro"/>

         <typeface name="Minion Pro"/>

         <typeface name="Courier Std"/>

         <typeface name="Adobe Pi Std"/>

         <typeface name="Adobe Hebrew"/>

         <typeface name="Adobe Arabic"/>

         <typeface name="Adobe Thai"/>

         <typeface name="Kozuka Gothic Pro-VI M"/>

         <typeface name="Kozuka Mincho Pro-VI R"/>

         <typeface name="Adobe Ming Std L"/>

         <typeface name="Adobe Song Std L"/>

         <typeface name="Adobe Myungjo Std M"/>

      </typefaces>

   </locale>

</localeSet>

<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">

   <xfa:data xfa:dataNode="dataGroup"/>

</xfa:datasets>

<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.398682, 2009/08/10-13:00:47        ">

   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

      <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" rdf:about="">

         <xmp:MetadataDate>2010-07-18T20:08:58Z</xmp:MetadataDate>

         <xmp:CreatorTool>Adobe LiveCycle Designer ES 9.0</xmp:CreatorTool>

      </rdf:Description>

      <rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/" rdf:about="">

         <pdf:Producer>Adobe LiveCycle Designer ES 9.0</pdf:Producer>

      </rdf:Description>

      <rdf:Description xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" rdf:about="">

         <xmpMM:DocumentID>uuid:76c87f42-7062-4fca-a1ed-205e34ae0e47</xmpMM:DocumentID>

         <xmpMM:InstanceID>uuid:fd63ecf7-eea9-40eb-9511-92e47192a28b</xmpMM:InstanceID>

      </rdf:Description>

      <rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/" rdf:about="">

         <dc:format>application/pdf</dc:format>

      </rdf:Description>

      <rdf:Description xmlns:desc="http://ns.adobe.com/xfa/promoted-desc/" rdf:about="">

         <desc:version rdf:parseType="Resource">

            <rdf:value>8.2.1.3158.1.475346.466429</rdf:value>

            <desc:ref>/template/subform[1]</desc:ref>

         </desc:version>

      </rdf:Description>

   </rdf:RDF>

</x:xmpmeta>

<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">

   <annots/>

</xfdf></xdp:xdp>

4 Replies

Avatar

Former Community Member

Take a look at the attached. I think I have it right although my FormCalc skills leave something to be desired.

// form1.page1.ageAsOfDate::calculate - (FormCalc, client)

if (HasValue(dob)) then

  if (HasValue(selectedDate)) then

    var selectedDate_ = Date2Num(selectedDate.formattedValue, "MM/DD/YYYY")

    var dob_ = Date2Num(dob.formattedValue, "MM/DD/YYYY")

    var diff = selectedDate_ - dob_

    $.rawValue = Floor(diff / 365.25)

  else

    $.rawValue = null

  endif

else

  $.rawValue = null

endif

Untitled.png

Steve

Avatar

Level 1

Hey, this works really great and I have a follow up question.  I set the date to calculate someones age as of October 1, 2010.  When they type in their birth date, that age pops up in the age box.  I want to use the age that pops up in that box to control some radio buttons later in my document.  For example, if they are under the age of sixteen, then certain buttons will be disabled.  The only way I can get this to work is to have the person click on there age after it is auto calculated.  That clicking activates the disabled command.  I want the command to initialize immediately after they type in there birthdate.  I only want the field where they can enter their birthdate on my form.  The rest will be invisible.  Is there a way to do that?  Thanks for your help.

Avatar

Former Community Member

Added a button and the following on the button calculate event...

// form1.page1.btn::calculate - (JavaScript, client)


form1.page1.btn.access = "open";

var age = parseInt(form1.page1.ageAsOfDate.rawValue);

if (age < 16) {

  form1.page1.btn.access = "protected";

}

Steve

Avatar

Former Community Member

Nearly there!  Many thanks for your simple little formula.  I got it working as follows:

if (HasValue(Page1.studentdetails.dob)) then
   if (HasValue(rdtestdate)) then
      var rdtestdate_ = Date2Num(rdtestdate.formattedValue, "DD/MM/YYYY")
      var dob_ = Date2Num(Page1.studentdetails.dob.formattedValue, "DD/MM/YYYY")
      var diff = rdtestdate_ - dob_
      $.rawValue = Floor(diff / 365.25)
   else
   $.rawValue = null
   endif
else
$.rawValue = null
endif

What I need is to report the results in terms of years and months rather than round to years only.  How can I get the result to be formatted as years and months?

Many thanks for your assistance!

jeannie