SOLVED - How to get someone's age from DOB and today's date

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
admin12
Posts: 412
Joined: Wed May 11, 2011 9:47 am

SOLVED - How to get someone's age from DOB and today's date

Post by admin12 » Sat Jul 30, 2011 10:07 pm

I need to figure out someone's age.

I have their date of birth and I have today's date. How do I get the end result. My math skills SUCK.

Here's my code (loaded in from the database with a SELECT statement):

put item 7 of myLine into dobMonth
put item 8 of myLine into dobDay
put item 9 of myLine into dobYear
put chartonum("dobYear") into dobYearNum
put the long system date into field "TodayDate"

I figured I would subtract this year from the dobyear and get the age - am I right? How would I do this if I am?

Mike
Last edited by admin12 on Sun Jul 31, 2011 5:03 pm, edited 1 time in total.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: How to get someone's age from DOB and today's date

Post by bangkok » Sun Jul 31, 2011 6:51 am

Go for the Date and Time Library, from Sarah.

http://www.troz.net/rev/index.irev?cate ... ary#stacks

You'll find a function : DaysBetween, that will do the work (returns the number of days between 2 dates).

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: How to get someone's age from DOB and today's date

Post by wsamples » Sun Jul 31, 2011 9:31 am

It's not so hard! You need to subtract the 4 digit birthday year from the four digit current year. Then you need to check whether or not the birth date (month and day) has been reached in the current year, and subtract a year if it hasn't. Here's an example of one way to do it

Code: Select all

  
   -- using your stored birthday information: 
    -- put the birth month into tBM
    -- put the birth day (of month) into tBD
    -- put 4 digit birth year into tBY
    
   -- get today's date in a format you can use:
   convert the seconds to dateItems
    put item 1 of it into thisYear
    put item 2 of it into thisMonth
    put item 3 of it into thisDay
    
    put thisYear - tBY into trawYears
    
    switch 
    case thisMonth < tBM
    case (thisMonth = tBM) and (thisDay < tBD)
    put trawYears - 1 into tAgeInYears
    break
    default 
    put tRawYears into tAgeInYears
    end switch
    
    -- tAgeInYears is your answer
Good luck!

Warren

admin12
Posts: 412
Joined: Wed May 11, 2011 9:47 am

Re: How to get someone's age from DOB and today's date

Post by admin12 » Sun Jul 31, 2011 4:09 pm

wsamples wrote:It's not so hard! You need to subtract the 4 digit birthday year from the four digit current year. Then you need to check whether or not the birth date (month and day) has been reached in the current year, and subtract a year if it hasn't. Here's an example of one way to do it

Code: Select all

  
   -- using your stored birthday information: 
    -- put the birth month into tBM
    -- put the birth day (of month) into tBD
    -- put 4 digit birth year into tBY
    
   -- get today's date in a format you can use:
   convert the seconds to dateItems
    put item 1 of it into thisYear
    put item 2 of it into thisMonth
    put item 3 of it into thisDay
    
    put thisYear - tBY into trawYears
    
    switch 
    case thisMonth < tBM
    case (thisMonth = tBM) and (thisDay < tBD)
    put trawYears - 1 into tAgeInYears
    break
    default 
    put tRawYears into tAgeInYears
    end switch
    
    -- tAgeInYears is your answer
Good luck!

Warren
Warren,

Thank you. I get an error on this line:

-- get today's date in a format you can use:
convert the seconds to dateItems

How do I get the current date "in a format I can use"?

Thank you so much for your help. This is one of the things that was stopping me from getting this done.

Mike

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: SOLVED - How to get someone's age from DOB and today's date

Post by wsamples » Sun Jul 31, 2011 5:22 pm

"convert the seconds to dateItems" should work, if you've typed it correctly (or copy/pasted). Could you post the error?

Try it in the message box., like this: convert the seconds to dateItems;put it

Post Reply