Re: A Difference of Months
Re: A Difference of Months
- Subject: Re: A Difference of Months
- From: Nigel Garvey <email@hidden>
- Date: Wed, 13 Mar 2002 03:30:53 +0000
Arthur J Knapp wrote on Tue, 12 Mar 2002 16:55:22 -0500:
>
So I need to find how many months difference there are between any
>
two dates.
I knew I shouldn't have downloaded any more mail at this time of the
morning...! ;-)
>
Currently, I am using ...
[snip]
>
Any ideas on a better way to do this? My current project involves taking
>
two dates, a birthday and the current date, and finding out how old someone
>
is. If they are 24 months or younger, I want to return a string that says
>
something like:
>
>
"This person is 15 months old."
>
>
where as if they are 25 or more months old, I simply want to return their
>
age in years:
>
>
"This person is 29 years old."
First thoughts: get the difference of the years, multiply by twelve, add
the difference of the month numbers. If the later date's day is less than
the earlier's, subtract 1.
on monthNum from dateObj -- "French Vanilla" variant
--> Hack to get round bug when subtracting one pre-1904 date from
another
copy dateObj to dateTemp
tell dateTemp's year
if it comes before 1904 then set dateTemp's year to it + 2000
end tell
-- End hack
copy dateTemp to dateTemp2
set month of dateTemp2 to January
return (1 + (dateTemp - dateTemp2) div 2.5E+6)
end monthNum
on DifferenceOfMonths(d1, d2)
-- No need to copy the dates. The originals aren't affected.
-- Ensure that d1 represents the later date
if (d1 < d2) then set {d1, d2} to {d2, d1}
set wholeMonths to ((d1's year) - (d2's year)) * 12 + (monthNum from
d1) - (monthNum from d2)
if d1's day comes before d2's day then set wholeMonths to wholeMonths
- 1
if wholeMonths < 25 then
"This person is " & wholeMonths & " months old."
else
"This person is " & (wholeMonths div 12) & " years old."
end if
end DifferenceOfMonths
DifferenceOfMonths(date ("9th June 2000" as string), current date)
--> "This person is 21 months old."
DifferenceOfMonths(date ("9th June 1790" as string), current date)
--> "This person is 211 years old."
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.