Created
December 20, 2011 09:45
-
-
Save epetousis/1500982 to your computer and use it in GitHub Desktop.
Calculate age by birth year with a simple algorithm. Very simple AppleScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set {year:y} to (current date) | |
display dialog "Enter your birth year." default answer "" | |
set birthdate to text returned of result | |
set age to y - birthdate | |
display dialog age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple, but I want to be more accurate about the age so I added in Month and day.
Pretty crude especially with dealing with days and months that have already passed and it doesn't account for leap years (would be fun to figure out). Trim it up if you can. I tend to take the long way around:
--SET CURRENT YEAR, MONTH, AND DAY TO VARIABLES Y, M, D
set {year:y, month:m, day:d} to (current date)
--QUERY USER TO ENTER THEIR BIRTHDAY INFORMATION
set BirthdayDay to text returned of (display dialog "Enter Day of Birth" default answer "")
set BirthMonth to (choose from list {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} with prompt "Please Choose Birthday Month")
set BirthYear to text returned of (display dialog "Enter four digit birth year" default answer "")
--TURN USER INFORMATION INTO DATE FORMAT
set UserBirthDay to BirthMonth & " " & BirthdayDay & "," & BirthYear as string
set Birthdate to date UserBirthDay
--CONVERT MONTH INTO A NUMBER (INTEGER)
set theMonthNumber to ((month of Birthdate as integer) as string)
--CALCULATE USERS AGE AND WHEN THE USERS NEXT BIRTHDAY IS
set UserAge to y - (year of Birthdate)
set daysleft to (day of Birthdate) - d
set monthleft to (theMonthNumber) - m
--CHECK IF DAYS LEFT ARE NEGATIVE NUMBER AND CONVERT INTO WHOLE NUMBER
if daysleft < 0 then
set daysleft to daysleft / -1 as integer
end if
--CHECK IF MONTH HAS ALREADY PAST
if monthleft < 0 then
set monthleft to monthleft + 12
else
set UserAge to UserAge - 1
end if
--SET TEXT TO SHOW USER WHEN BIRTHDAY IS
set BirtdayInfo to "You are currently...
" & UserAge & " years old
" & "There are...
" & monthleft & " months and
" & daysleft & " days " & "left until your birthday!"
--CHECK TO SEE IF TODAY IS THE USERS BIRTHDAY
if monthleft = 0 and daysleft = 0 then
set BirtdayInfo to "HAPPY BIRTHDAY!"
end if
display dialog BirtdayInfo