Thread: convert age in months to years,months,days

1. convert age in months to years,months,days

Use calculation below to calculate age in months on input of birth date in "DOB" field -writes to a field for age in months.
12*(YEAR(SHOW_DATE)-YEAR(DOB))+MONTH(SHOW_DATE)-MONTH(DOB)+IF(DAY(DOB)>=DAY(SHOW_DATE),1,0)

Need the age in months converted to years,months,days to be written in another field called "aginyears"
or
With input of birth date in DOB field to calclulate age in years,months,days for field "aginyears"

Any help would be appreciated
Jim

2. Re: convert age in months to years,months,days

Jim,

This to start you off.

Age=date()-DOB
months=age*12

You will need to make some adjustment between the number of months in the DOB year and number of months to date in the current year.

3. Re: convert age in months to years,months,days

I have a post in the code archive forum

http://msgboard.alphasoftware.com/al...onths-and-days

see if that is useful.

4. Re: convert age in months to years,months,days

Mr. Gandri,
Your code was exactly what I was looking for. I knew that I had seen on the message board but was unable to find it. Thanks!

However now upon executing "New Record" (a button on menu for adding a new animal) the dialog box for the year,month pops up.
Can not get past it to enter information about the animal.
Can you help?
Jim

5. Re: convert age in months to years,months,days

take a look at this:
I normally do not work on desktop, my comfort level is not that good with desktop. so please bear with me.
http://screencast.com/t/G8fKAM1dUI
Code:
```FUNCTION alpha_ageFull2 AS C (dateOfBirth AS D, showDate AS D )

if dateOfBirth > showDate then
alpha_ageFull2 = "Date of birth is later than the end date!"
else
dim y1 as n
dim y2 as n
dim m1 as n
dim m2 as n
dim d1 as n
dim d2 as n
dim dY as n
dim dM as n
dim dD as n
dim leapYr as l

y1 = year(dateOfBirth)
y2 = year(showDate)
m1 = month(dateOfBirth)
m2 = month(showDate)
d1 = day(dateOfBirth)
d2 = day(showDate)

select
case mod(y2,4)>0
leapYr = .f.
case mod(y2,400)=0
leapYr = .t.
case mod(y2,100)=0
leapYr = .f.
case else
leapYr = .t.
end select

if ( d2-d1 < 1)
select
case m2 = 1
d2 = d2+31
case m2 = 2
if leapYr
d2 = d2+29
else
d2 = d2+28
end if
case m2 = 3
d2 = d2+31
case m2 = 4
d2 = d2+30
case m2 = 5
d2 = d2+31
case m2 = 6
d2 = d2+30
case m2 = 7
d2 = d2+31
case m2 = 8
d2 = d2+31
case m2 = 9
d2 = d2+30
case m2 = 10
d2 = d2+31
case m2 = 11
d2 = d2+30
case m1 = 12
d2 = d2+31
end select
m2 = m2-1
end if
if (m2 < m1)
y2 = y2-1
m2 = m2+12
end if
dY = y2-y1
dM = m2-m1
dD = d2-d1
alpha_ageFull2 = "Age: "+dy+" years " +dM+" months "+dD+" days "
end if

END FUNCTION```
http://screencast.com/t/e9ZRs2ee
improved screencast
corrected the code and the screencast

6. Re: convert age in months to years,months,days

Mr. Gandhi,
Again thanks for your help. I have implemented the code into my application and it works.
I am having a little difficulty with making the script run like I would like to have it run.
Attached is my application.
On input of llama birth date-would like for it to populate the code with startdate (which is "DOB" field) and enddate (which has obtained from "show_date" field ) and then display age in years, month,days.
This would do the task in one operation as opposed to two as I now have.

Please understand that I am a novice at this and sometimes may not use the correct terminology.
Jim

7. Re: convert age in months to years,months,days

I will redo your database and send back.
in the meantime
take a look at the second video below the code and study the code. I revised earlier after rereading your post.
you have copied the xdialog method, but I am using function and calling the function from on change event on your show_date field.
also I will be changing your age_in_years from numeric to character to show the string.
it will take about 20 minutes or so then I will upload the zipped file.

8. Re: convert age in months to years,months,days

okay
I did some modifications to the table and the form.
I changed the yearsold to character field, I changed the animal_age from calculated field to user entered, I removed the button to calculate the age, I removed the event firing the xdialog on canedit event of the show_date. I changed the tab order to suit me, that is not my advice for you to change.

here are the two videos
llama1
http://screencast.com/t/ERbG5NdOdw

llama2
http://screencast.com/t/lirp5EeA

Please understand that I am a novice at this and sometimes may not use the correct terminology.
work looks very good to me.

9. Re: convert age in months to years,months,days

Again Mr. Gandhi thank you for your input.
I have changed the tab order to suit a better flow of data input.
Changed the show_date so that it defaults to the last entry. When using the application the show _date will stay the same until you start a new set of show data for a different show. By changing to default you are not having to input show_date for every new animal entered. The only thing I can not solve is upon changing the DOB (because of wrong input by user-typo) for an animal already in the data base the code will not recalculate the age. I have tried several different things-never recalculates age.
Attached latest zip

Jim

10. Re: convert age in months to years,months,days

because the show_date does not change the onChange event will not fire if attached to that field. you could on the otherhand attach to the dob of the animal field on change event then it should calculate when you enter the dob.

11. Re: convert age in months to years,months,days

okay I made changes to the form.
now on change event in on date of birth field. tab order is changed so that the show date is filled in before tha dob is filled in.

12. Re: convert age in months to years,months,days

if you think, you may need to change the show_date periodically on the data then you can have same function fire onChange event of that field too.
you can modify the code for the function to skip calculation if either of the fields is empty. if you do that then you can keep the current tab order.

13. Re: convert age in months to years,months,days

The tab order in each example works fine. I never gave it a though that the tab order effected running the code. Learned something the last few days.
This message board and it's advanced "Certified Alphaholic" are amazing.
Again thinks again.
Jim

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•