Age Monitoring

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Age Monitoring

Post by lemodizon » Sat Aug 10, 2019 5:27 pm

Hello Everyone,

I'm trying to practice the dates in livecode its kinda tricky. I was able to find a script that would get the age upon learning dates on livecode. here is the script below.

Questions

1. I want to put a button that will check the birth date on my datagrid and will automatic increment the age once it reached it's birth date and it will update into my database (sqlite) but I don't know where to start

2. Is there a shorter script code on how to get the age? aside from the script below.

Thanks

Code: Select all

on mouseUp  --age
   
   
   put fld "BirthDate" into tBirthdate
   if tBirthdate = empty then  answer "no entered birthdate" 
   
   if number(words in tBirthdate) = "1" and item 1 of tBirthdate is a number and item 1 of tBirthdate >= "1" and item 1 of tBirthdate <= "12" and \
         item 2 of tBirthdate is a number and item 2 of tBirthdate >= "1" and item 2 of tBirthdate <= "31" and \
         item 3 of tBirthdate is a number and length(item 3 of tBirthdate) = "4" then
      set the itemDel to ","  --default
      
   end if
   
     put age(tBirthdate) into fld "Age"
  
end mouseUp




function age Birthdate  --age("mm/dd/yyyy")  [4-digit year] [handles windows dates < 1/1/1970]
   
   local NewerYear, NewerMonth, NewerDay, OlderYear, OlderMonth, OlderDay, AgeYear, AgeMonth, AgeDay
   
   convert date() to dateItems
   put item 1 of it into NewerYear
   put item 2 of it into NewerMonth
   put item 3 of it into NewerDay
   set the itemDel to "/"
   
   put item 1 of Birthdate into OlderMonth
   put item 2 of Birthdate into OlderDay
   put item 3 of Birthdate into OlderYear
   set the itemDel to ","  --default
   
   put NewerYear - OlderYear into AgeYear
   
   if NewerMonth < OlderMonth or NewerMonth = OlderMonth and NewerDay < OlderDay then subtract 1 from AgeYear
   
   put NewerMonth - OlderMonth into AgeMonth
   
   if AgeMonth < "0" or NewerDay < OlderDay then add 12 to AgeMonth
   
   put NewerDay - OlderDay into AgeDay
   
   if AgeDay < "0" then
      subtract 1 from AgeMonth
      add 30 to AgeDay  --approximate
   end if
   return AgeYear
   
end age
Attachments
Capture11.PNG
Capture11.PNG (10.5 KiB) Viewed 7601 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Age Monitoring

Post by [-hh] » Sun Aug 11, 2019 12:49 am

The following switches the age in full years exactly with the first second of the birthday (or may be of the day after in case of Febr 29). Respects daylight saving and UTC offset (via using the internet date).

Code: Select all

function age d
  put d into d1; put the internet date into d2
  if d1 is a date then
    convert d1 to dateItems
    convert d2 to dateItems
    put "0,0,0,0" into item 4 to 7 of d2 
    put item 1 of d2-item 1 of d1 into dy
    add dy to item 1 of d1
    convert d1 to seconds; convert d2 to seconds
    if d1 > d2 then return dy-1
    else return dy
  else return "Not a date."
end age
Example usage:

Code: Select all

on mouseUp
  put age(fld "birthdate") into fld 1
end mouseUp
shiftLock happens

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sun Aug 11, 2019 12:19 pm

[-hh]

Thanks for the script code i already tried and it works. still figuring out on how to increment by 1 when it reach its birthday .
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Age Monitoring

Post by dunbarx » Sun Aug 11, 2019 1:48 pm

Hi.

Hermann is our staff mathematician. Always trust him.

But I wonder if, just for teaching purposes, you might be try stepping through this:

Code: Select all

on mouseUp
    ask "Birthday?" with "dd/mm/yyyy"
   if it is not a date then exit mouseup
   
   convert it to seconds ; put it into birthSeconds
   convert the date to seconds ; put it into currentSeconds
   answer trunc((currentSeconds - birthSeconds) / 31356000 - 1)
end mouseUp
You must enter a four-digit year. "31356000" is the number of seconds in a year. Leap years are included.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Age Monitoring

Post by [-hh] » Sun Aug 11, 2019 2:40 pm

... Always trust him.
... to make a a little bit less errors than usual.

Craig,
you probably mean an approximation like using 365.25 days for a year. This is also (mostly) OK.
But 31356000/24/60/60 = 362.917 days. So where's your typo?

lemodizon,
say the difference between the current year and the birth year is dy=42.
Then you compute the first second of the birtdate+42 years and convert it to seconds (LC does when converting all the 'leap-corrections' for us). This is d1.
Then compute the first second of the current date, this is d2.
The age jumps from dy-1 to dy if d1 <= d2 (that is when d1=d2 if checking in that second).
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Age Monitoring

Post by dunbarx » Sun Aug 11, 2019 4:23 pm

Hermann.

Typos are always in my head.

365.2425 * 86400 is better.

Craig

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sun Aug 11, 2019 5:03 pm

[-hh] wrote:
Sun Aug 11, 2019 2:40 pm
... Always trust him.
... to make a a little bit less errors than usual.

Craig,
you probably mean an approximation like using 365.25 days for a year. This is also (mostly) OK.
But 31356000/24/60/60 = 362.917 days. So where's your typo?

lemodizon,
say the difference between the current year and the birth year is dy=42.
Then you compute the first second of the birtdate+42 years and convert it to seconds (LC does when converting all the 'leap-corrections' for us). This is d1.
Then compute the first second of the current date, this is d2.
The age jumps from dy-1 to dy if d1 <= d2 (that is when d1=d2 if checking in that second).
Hermann,
Thank you very much i will try it thanks for the advice. :D
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sun Aug 11, 2019 5:05 pm

dunbarx wrote:
Sun Aug 11, 2019 1:48 pm
Hi.

Hermann is our staff mathematician. Always trust him.

But I wonder if, just for teaching purposes, you might be try stepping through this:

Code: Select all

on mouseUp
    ask "Birthday?" with "dd/mm/yyyy"
   if it is not a date then exit mouseup
   
   convert it to seconds ; put it into birthSeconds
   convert the date to seconds ; put it into currentSeconds
   answer trunc((currentSeconds - birthSeconds) / 31356000 - 1)
end mouseUp
You must enter a four-digit year. "31356000" is the number of seconds in a year. Leap years are included.

Craig
Craig,

Thank you :D
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Fri Aug 16, 2019 4:51 am

Hello,

Thanks to Hermman and Criag for teaching me on how to get the current age using livecode.

i created a function daysbetween that will determine the remaining days before his/her birthday but i'm stuck.

I want display if he/she reached its birthday it will answer Happy Birthday and it will add 1 from its old age. else if its not his/her birthday it will display the remaining days before its birthday. i don't know if this fuction i created is correct procedure.


Sorry Hermann i wasn't able to get your instruction regarding on this
-lemodizon,
say the difference between the current year and the birth year is dy=42.
Then you compute the first second of the birtdate+42 years and convert it to seconds (LC does when converting all the 'leap-corrections' for us). This is d1.
Then compute the first second of the current date, this is d2.
The age jumps from dy-1 to dy if d1 <= d2 (that is when d1=d2 if checking in that second).

Code: Select all


local dy

function age d
  put d into d1; put the internet date into d2
  if d1 is a date then
    convert d1 to dateItems
    convert d2 to dateItems
    put "0,0,0,0" into item 4 to 7 of d2 
    put item 1 of d2-item 1 of d1 into dy
    add dy to item 1 of d1
    convert d1 to seconds; convert d2 to seconds
    if d1 > d2 then return dy-1
    else return dy
  else return "Not a date."
end age


####################

on mouseUp
  put age(fld "birthdate") into "Age"
  put  DaysBetween (fld "BirthDate") into fld "Days"
end mouseUp


################################### 


function DaysBetween pDateBirthDate 
   
   put pDateBirthDate into tBirthDate
   put  the date into tDateCurrent
   
   if tBirthDate is a date then
      
      convert  tBirthDate to dateitems
      add dy to item 3 of tBirthDate
      convert tBirthDate to seconds
      put tBirthDate into tBirthDateSeconds
      
      convert tDateCurrent to dateitems
      convert tDateCurrent to seconds
      put tDateCurrent into tDateCurrentSeconds
      
   ##### I'm stuck here   
      if tBirthDateSeconds <= tDateCurrentSeconds then
         return dy
      end if
      
   end if
end DaysBetween
Attachments
Capture11.PNG
Capture11.PNG (7.02 KiB) Viewed 7304 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Age Monitoring

Post by Klaus » Fri Aug 16, 2019 10:51 am

Hi Lemuel,

Code: Select all

...
add dy to item 3 of tBirthDate
...
"item 3 of tBirthDate" = the day of the month!
See dictionary -> dateitems


Best

Klaus

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Age Monitoring

Post by [-hh] » Fri Aug 16, 2019 12:18 pm

You could try the following.

Code: Select all

on mouseUp
  put ageAndDays(fld "birthdate") into x
  put item 1 of x into fld "Age"
  put item 2 of x into fld "Days"
end mouseUp

-- Returns the age in full years at the first second of the
-- birthday and the number of days until the next birthday
-- incl. the current day.
-- Respects daylight saving and UTC offset (via internet date).
function ageAndDays d
  put the internet date into d0
  put d into d1
  put d into d2
  if d1 is a date then
    convert d0 to dateItems
    convert d1 to dateItems
    convert d2 to dateItems
    put (item 3 of d1 is item 3 of d0) into sameDay
    put item 1 of d0-item 1 of d1 into dy
    add dy to item 1 of d1
    add dy+1 to item 1 of d2
    put "0,0,0,0" into item 4 to 7 of d2 
    convert d0 to seconds
    convert d1 to seconds
    convert d2 to seconds
    if d1 > d0 then
      return dy-1, 1+((d1-d0) div 86400)
    else
      put 1+((d2-d0) div 86400) into dDiff
      if sameDay and dDiff > 364 then
        return dy, "Happy Birthday"
      else return dy, dDiff
    end if
  else return "Not a date."
end ageAndDays
shiftLock happens

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sat Aug 17, 2019 2:41 pm

Klaus wrote:
Fri Aug 16, 2019 10:51 am
Hi Lemuel,

Code: Select all

...
add dy to item 3 of tBirthDate
...
"item 3 of tBirthDate" = the day of the month!
See dictionary -> dateitems


Best

Klaus

Hello Klaus,

Sorry for the wrong script. i'm having a hard time in studying Dates & Time in livecode. i will check the dictionary thanks for the support.

Regards,

Lemuel
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sat Aug 17, 2019 3:29 pm

Hello Hermann,

It works :D

First of all Thank you for the walk-through and for guiding me on how to deal dates in livecode. I already tested and study your script code via debug mode so that i can see how it works. i added some script code

Code: Select all

on mouseUp
  put ageAndDays(fld "birthdate") into x
  put item 1 of x into fld "Age"
  put item 2 of x into fld "Days"
end mouseUp

-- Returns the age in full years at the first second of the
-- birthday and the number of days until the next birthday
-- incl. the current day.
-- Respects daylight saving and UTC offset (via internet date).
function ageAndDays d
  put the internet date into d0
  put d into d1
  put d into d2
  if d1 is a date then
    convert d0 to dateItems
    convert d1 to dateItems
    convert d2 to dateItems
    put (item 3 of d1 is item 3 of d0) into sameDay
    put item 1 of d0-item 1 of d1 into dy
    add dy to item 1 of d1
    add dy+1 to item 1 of d2
    put "0,0,0,0" into item 4 to 7 of d2 
    convert d0 to seconds
    convert d1 to seconds
    convert d2 to seconds
    if d1 > d0 then
      return dy-1, 1+((d1-d0) div 86400)
    else
      put 1+((d2-d0) div 86400) into dDiff
      if sameDay and dDiff > 364 then
      
 ######## I added this script below once it reached it birthday it will add 1 to our age.
 ######    i don't know if i'm right to put it here.
            
            add 1 to dy  
            
        return dy, "Happy Birthday"
      else return dy, dDiff
    end if
  else return "Not a date."
end ageAndDays

Question do i have to add another date for checking if its birth date has been celebrated? i experiment your code the result is this. or do i have to another condition.

Code: Select all

 if d0 > d1 then
         return dy-1, 1+((d1-d0) div 86400)
      else
         put 1-((d2-d0) div 86400) into dDiff
     
         if sameDay and dDiff < 364 then
            return dy, "belated happy birthday"
         else return dy, dDiff
      end if
below picture i tried to entered the birthdate:08/18/2018 the age won't add and the number of days it comes back to 1.

Thanks Hermann

Thank you LiveCode
Attachments
Capture11.PNG
Capture11.PNG (5.77 KiB) Viewed 7163 times
Capture.PNG
Capture.PNG (5.48 KiB) Viewed 7163 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Age Monitoring

Post by [-hh] » Sat Aug 17, 2019 5:06 pm

If I understand correctly what you want then you could check that with another field "gotCake" for that user:

Code: Select all

on mouseUp
  put ageAndDays(fld "birthdate") into x
  put item 1 of x into fld "Age"
  put item 2 of x into fld "Days"
  if item 2 of x is "Happy Birthday" then
    put true into fld "gotCake"
  else if item 2 of x > 358 then -- up to 7 or 8 days late
    if fld "gotCake" is not true then -- congratulate
      put ": Belated Happy Birthday" after fld "Days"
      put true into fld "gotCake"
    end if
  else put false into fld "gotCake"
end mouseUp
For that my function ageAndDays is unchanged as above.
[Note. Using above "not true" includes also an empty fld "gotCake" (because empty is not true).]
shiftLock happens

lemodizon
Posts: 175
Joined: Thu Apr 05, 2018 3:33 pm

Re: Age Monitoring

Post by lemodizon » Sun Aug 18, 2019 1:34 pm

Hello Hermann,

Thank you very much it works :) Thanks for the walk-through in livecode(Dates).

Long live Team LiveCode!
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”