How can i calculate age from years? Like this:
17-10-1981 (this paste in "field1") and show the years 39 in "field2"
How can i calculate age from years
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: How can i calculate age from years
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
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
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
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
-
- Posts: 738
- Joined: Thu Sep 11, 2014 1:49 pm
Re: How can i calculate age from years
Take a look here for date and time calculations
http://livecode.byu.edu/time/timeInRev.php
http://livecode.byu.edu/time/timeInRev.php
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: How can i calculate age from years
I am a simpler type of person:
- -
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.
--------
The original OP will have a "merry dance" switching between 2 itemDelimiters ("/" and "-").
- -
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
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.

--------
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