12 Replies Latest reply on Aug 9, 2010 12:15 PM by jandob

    How do you calculate a field

    jandob

      Hi all,

      I would really appreciate some help as I do not know programing, java script or other programming languages.  I need to have a field calculate for me.

      The form has date of birth. Then I need the next field, (a text field) calculate the age on the previous field of the Date of Birth.  Does anyone have an idea how to do that?  I have gone through the help guide, but it assume you know the langauage to put in.

      thanks in advance.

        • 1. Re: How do you calculate a field
          Kevin Cavallo Level 4

          How do you need the age displayed - just whole years?

          • 2. Re: How do you calculate a field
            jandob Level 1

            yes in whole years.

            based upon the date in the previous field.  Did you need to see the form?

             

            Janet

            • 3. Re: How do you calculate a field
              Kevin Cavallo Level 4

              Nope.  I've attached a demo form with the code in it.  Calculating ages is pretty simple since you don't need to deal with leap years and how many days in a month.  You turn one year older on a certain month/day. If it's past that month day, you're one year older regardless of how many days were in the previous year.  So, the code subtracts the current year from the birth year.  Then if the current month/day is less than the birth month/day we subtract one from the year since we haven't yet reached the birthday for this year.

               

              The code in the attached form is javascript and it's in the 'calculate' event for the age field.  There is no checking done to make sure that the birth date field is a valid date, so I'll leave that up to you.  Let me know if you need help with that, but the Adobe controls should handle that without any script.  I'm in the US so I tested it using US format dates, e.g. m/d/yyyy.

               

              Kevin

              1 person found this helpful
              • 4. Re: How do you calculate a field
                jandob Level 1

                Hi Kevin,

                 

                That is helpful to have the beginning of the answer. I pasted the code that you had into my age field and it did not display the age. I am presuming two things may have occurred. Can you help me with these?

                 

                1. the calculation says "get birth date".  How do I tell it which field is the birth date field?  on my form it is the field one tab before the age field. Since you created the formula and I just pasted it, I am not sure it is relating to the field.

                 

                2.  the second cacluation says "get today's date". this is the same issue as how does your formula know where to find today's date field on the form?  What must I do to make the formula mine.

                 

                Questtion: Does the green text mean anything special, separate from blue text or black text?

                 

                Again, I am not a programmer and it all looks like Greek to me except that I can read what you are looking for.

                So my basic question is: what must I do to the formula you attached to make it work in my form.  I have also used the US date format.  Thanks for thinking of that, as I am in the US.  I do appreciate your help more than you can know.

                 

                I am presenting this form tomorrow for training.  I don't need to have this done by tomorrow, but it would be great if I could.

                 

                Regards,

                Janet

                 

                below is a copy of the form.

                • 5. Re: How do you calculate a field
                  Kevin Cavallo Level 4

                  Janet, I've fixed the form and attached the updated version.  There were a couple of things that I changed:

                   

                  1. The form was saved as a static .pdf form.  This means that it's for display only; you can't run any scripts and change things programatically.  I changed it to be a dynamic form, which allows scripts to run when the user is entering data.  To do this I selected Save As from the file menu and then selected Save as type: Adobe Dynamic XML Form (*.pdf).

                   

                  2. Since the form was saved as static it stripped out the scripts.  I re-pasted the script that I gave you back into the calculate event for the Age field.

                  I also changed the Language dropdown on the upper right side of the script window to Javascript, since that's the language that I used.

                   

                  3. The name of your DOB field is DateTimeField1.  You can see this by either looking at the Hierarchy window or by looking at the Field tab of the Object window.  In the future you should change the names of the fields from their default to something more descriptive.  For now it's fine.  I then replaced my references to "DOB" in my script with "DateTimeField1.

                   

                  4. Make sure that the preview type is set to dynamic.  Go to the File menu in designer and select the Form Properties item.  Click on the Preview tab and choose "Preview Type": Interactive Form and "Preview Adobe XML Form as": Dynamic XML form.

                   

                  The above changes made it work, but I changed one more thing:  The type of the DOB field is set to DateTime field, and when you use the pop-up calendar to select the date it gets saved in the data in a backward format (YYY-MM-DD).  This doesn't show up on the form since you have the display format set, but behind the scenes it messes up the date calculation in the script.  I set the edit pattern for the field to be either D/M/YYYY or D-M-YYYY to allow some flexibility when the user enters the date, and to force it to save in the proper format for the script.

                   

                  Oh, forgot to mention that "new Date()" gets the current date from the system, so you don't need a field on your form for it.  Green is a comment, not part of the script but just to document what's happening in the script.  Blue means reserved script instruction, black is everything else.  Makes more sense to you if you're a programmer. The Livecycle editor automatically colors the text to make it easier to read, we just type it in.

                   

                  Give this a try and let me know if you have more issues.

                   

                  Kevin

                  • 6. Re: How do you calculate a field
                    jandob Level 1

                    Kevin,

                    Thanks so much!   I am still new to Live Cycle and it is mostly hit or miss. So far everything has worked, so this was the first glitch I had.

                     

                    Your answer was extensive, although I had to read it twice to be sure I understood. If i keep the form as pdf at this point, can I still make changes in live cycle without interfering with the changes you made?  I am hoping so.  But you really helped.

                     

                    I am forever grateful!

                     

                    Janet

                    • 7. Re: How do you calculate a field
                      abinathan

                      hi

                       

                      wit the above script for calculating the age.If you enter in dd/mm/yyyy it shows the age less than the current age.If you enter in yyyy/mm/dd it shows the correct age.Y is it so?

                       

                       

                       

                      cheers

                      abi

                      • 8. Re: cal the age
                        abinathan Level 1

                        hi

                         

                        wit the above script for calculating the age.If you enter in dd/mm/yyyy it shows the age less than the current age.If you enter in yyyy/mm/dd it shows the correct age.Y is it so?

                         

                        Attachments:

                         

                         

                        cheers

                        abi

                        • 9. Re: cal the age
                          Kevin Cavallo Level 4

                          The date object and script that you're using is set up to accept US-format dates (mm/dd/yyyy). You're entering the dates as dd/mm/yyyy, which throws off the conversion.  yyyy/mm/dd will work because it's still US-format (month before date).

                           

                          You can try changing the patterns used in the DOB field to D/M/YYYY and see if that works.  It may not, depending on how your system's locale is configured and how the javascript Date.parse() method works.  I set my machine's settings to German, but the parsing still interpreted the input as m/d/yyyy.

                           

                          If changing the patterns does not work, repost and we'll try something else.

                          • 10. Re: cal the age
                            abinathan Level 1

                            Hi Kevin

                                 

                                      I have changed the DOB field even though its not working.Could you please tell me if i give input as DD/MM/YYYY.Thanks in advance.

                             

                             

                             

                             

                             

                            Regards

                            Abi

                            • 11. Re: cal the age
                              Kevin Cavallo Level 4

                              Delete everything in the Calculate event of the Age object and paste in the following script.  It will allow the date format to be "DD/MM/YYYY".  Make sure that you change the script language dropdown to "FormCalc":

                               

                               

                               

                               

                              if ( DateTimeField1.rawValue <> null ) then

                                  var ageInYears = 0

                                  var curDate = Date()

                                  var curYear = Num2Date(curDate, "YYYY")

                                  var curMonth = Num2Date(curDate, "M")

                                  var curDay = Num2Date(curDate, "D")

                                  var birthDate = Date2Num(DateTimeField1.editValue, "D/M/YYYY")

                                  var birthYear = Num2Date(birthDate, "YYYY")

                                  var birthMonth = Num2date (birthDate, "M")

                                  var birthDay = Num2date (birthDate, "D")

                               

                                  ageInYears = curYear - birthYear

                                  if ( curMonth < birthMonth ) then

                                      ageInYears = ageInYears - 1

                                  endif

                               

                                  if ( curMonth == birthMonth ) then

                                      if ( curDay < birthDay ) then

                                          ageInYears = ageInYears - 1

                                      endif

                                  endif

                               

                                  $.rawValue = ageInYears

                              endif

                              • 12. Re: cal the age
                                jandob Level 1

                                Kevin,

                                I did not need the advice this time, but it was great to see you follow up with another person having a smiliar problem with the same issue I did.  I just wanted you to know that we have been using the form since you helped us fix it. It has been working like a dream.  I love it.  Thanks so much for your help in this matter.

                                Regards,

                                Janet