Calculating dates

yves COPPE yvescoppe at skynet.be
Sun Apr 27 03:05:01 EDT 2003


>Hi everyone
>
>I have been playing with this but can't get it to work. I want to 
>calculate ages of students from dates of birth and current date. If 
>I convert these dates to seconds, subtract the birth date from the 
>date entered - is there a simple way to then convert this number 
>into years, months and days?
>
>cheers
>
>Greg
>


Here is a script :

answer whichage(<yourbirthday>,<today>)

answer whichage(<"22/12/1967","27/03/2003")

function whichage fromwhich,towhich
   set the centuryCutoff to 01
   set useSystemDate to true
   if param(1) = "" then exit to top
   put param(1) into startdate
   put startdate into temp
   get last char of temp
   repeat until it is not a integer
     get last char of temp
     delete last char of temp
   end repeat
   if (it ‚ "-") and (it ‚ "/") then exit to top
   if param(2) = "" then
     put mi_shortdate() into enddate
   else put param(2) into enddate
   put enddate into temp
   get last char of temp
   repeat until it is not a integer
     get last char of temp
     delete last char of temp
   end repeat
   if (it ‚ "-") and (it ‚ "/") then exit to top
   convert startdate to dateitems
   convert enddate to dateitems
   put (item 3 of enddate - item 3 of startdate) into diffjour
   if diffjour < 0 then
     add "1" to item 2 of startdate
     get item 2 of enddate
     if (it is in "1,3,5,7,8,10") or (it= "12") then put "31" into correction
     else if (it is in "4,6,9,11") then put "30" into correction
     else if item 1 of enddate mod 4 = 0 then put "29" into correction
     else put "28" into correction
     add correction to item 3 of enddate
     put (item 3 of enddate - item 3 of startdate) into diffjour
   end if
   put (item 2 of enddate - item 2 of startdate) into diffmois
   if diffmois < 0 then
     add "1" to item 1 of startdate
     put (item 2 of enddate) + 12 into item 2 of enddate
     put (item 2 of enddate - item 2 of startdate) into diffmois
   end if
   put (item 1 of enddate - item 1 of startdate) into diffan
   return diffan&","&diffmois&","&diffjour
end whichage


Hope this help.
-- 
Greetings.

Yves COPPE

Email : yvescoppe at skynet.be



More information about the use-livecode mailing list