How can i calculate age from years

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
SEAL29
Posts: 63
Joined: Fri Oct 02, 2020 3:32 pm

How can i calculate age from years

Post by SEAL29 » Sat Oct 31, 2020 9:09 pm

How can i calculate age from years? Like this:
17-10-1981 (this paste in "field1") and show the years 39 in "field2"

SparkOut
Posts: 2946
Joined: Sun Sep 23, 2007 4:58 pm

Re: How can i calculate age from years

Post by SparkOut » Sun Nov 01, 2020 1:11 pm

First, you need to make sure that you present the date in a format that is recognised as a date in LiveCode.
Check out the "set the useSystemDate to true" option to see what the user's expected date format is. dd-mm-yyyy is not a universal format and you will need to error trap for the correct formatting and validate that your input is actually a genuine date, and that 4th July is not mistaken for April 7th, etc.
Once you have a date, look up "convert" and "dateItems". That will get you a comma separated list which you can add and subtract from each other. You can subtract 24 from the "months" item and LiveCode will internally work out that means taking 2 years off. This is handy because although the years may be (say) 7 apart, there might not be a whole 7 years between some people born in different months.
Converting to "seconds" and subtracting, then converting back may also be useful.

The big bugbear is that on Windows, LiveCode does not understand dates before 1970. So for calculations with dates of birth of older people, it is no use at all. This is one of my personal peeves.

To ease this, and benefit from other features, look for Malte's date/time library, mentioned in this thread http://forums.livecode.com/viewtopic.php?f=7&t=33298

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10319
Joined: Wed May 06, 2009 2:28 pm

Re: How can i calculate age from years

Post by dunbarx » Mon Nov 02, 2020 5:15 am

Hi.

What Sparkout said.

You may be interested to know that you can, at least on a Mac, use a considerably greater spread of time than generally published. See this:
http://forums.livecode.com/viewtopic.ph ... ng#p138530

This thread is silly yet both interesting and informative.

Craig

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: How can i calculate age from years

Post by mrcoollion » Mon Nov 02, 2020 9:04 am

Take a look here for date and time calculations
http://livecode.byu.edu/time/timeInRev.php

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: How can i calculate age from years

Post by richmond62 » Mon Nov 02, 2020 12:48 pm

I am a simpler type of person:
-
Screenshot 2020-11-02 at 13.45.51.png
-

Code: Select all

on mouseUp
   put the date into DATATA
   set the itemDelimiter to "/"
   put item 1 of DATATA into MESETS
   put item 2 of DATATA into DEN
   put ((item 3 of DATATA) + 2000) into GODINA
   ask "When were you born? Enter data in this form dd/mm/yyyy."
   put it into BIRTH
   put item 1 of BIRTH into BDEN
   put item 2 of BIRTH into BMESETS
   put item 3 of BIRTH into BGODINA
   ----
   put (GODINA - BGODINA) into ANNEE
   put (MESETS - BMESETS) into MONAT
   If MONAT < 0 then
      subtract 1 from ANNEE
   end if
   put (DEN - BDEN) into JOUR
   --
   put "You are " & ANNEE && "years old" into fld "fOUTPUT"
end mouseUp
This code presumes that LiveCode will get an American date from the computer (mm/dd/yy),
and asks the end-user to enter a European date (dd/mm/yyyy): of course you can muck the code
around as much as you like to serve your ends. 8)

--------

The original OP will have a "merry dance" switching between 2 itemDelimiters ("/" and "-").
Attachments
OLD SAUSAGE.livecode.zip
Here's the stack
(1.16 KiB) Downloaded 220 times

Post Reply