Page 1 of 1

How can i calculate age from years

Posted: Sat Oct 31, 2020 9:09 pm
by SEAL29
How can i calculate age from years? Like this:
17-10-1981 (this paste in "field1") and show the years 39 in "field2"

Re: How can i calculate age from years

Posted: Sun Nov 01, 2020 1:11 pm
by SparkOut
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

Re: How can i calculate age from years

Posted: Mon Nov 02, 2020 5:15 am
by dunbarx
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

Re: How can i calculate age from years

Posted: Mon Nov 02, 2020 9:04 am
by mrcoollion
Take a look here for date and time calculations
http://livecode.byu.edu/time/timeInRev.php

Re: How can i calculate age from years

Posted: Mon Nov 02, 2020 12:48 pm
by richmond62
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 "-").