A Difference of Months
A Difference of Months
- Subject: A Difference of Months
- From: Arthur J Knapp <email@hidden>
- Date: Tue, 12 Mar 2002 16:55:22 -0500
So I need to find how many months difference there are between any
two dates. If it was a question of days, it would be very simple:
set myLastBath to date ("" & "1-1-2002")
set todaysDate to current date
DifferenceOfDays(myLastBath, todaysDate)
on DifferenceOfDays(d1, d2)
-- protect dates, (dates are data-sharing objects)
--
copy {d1, d2} to {d1, d2}
-- We need to subtract the newer date from the older,
-- so ensure that d1 is newer:
--
if (d1 < d2) then set {d1, d2} to {d2, d1}
return (d1 - d2) div 60 div 60 div 24 --> difference in days
end DifferenceOfDays
Months are not as simple, however, because they have a differing
number of days. The only *simple* technique that can be used is to
find an approximation of months difference:
on DifferenceOfMonths(d1, d2)
-- protect dates, (dates are data-sharing objects)
--
copy {d1, d2} to {d1, d2}
-- We need to subtract the newer date from the older,
-- so ensure that d1 is newer:
--
if (d1 < d2) then set {d1, d2} to {d2, d1}
-- Approximation:
--
return (d1 - d2) div 60 div 60 div 24 div 31
end DifferenceOfMonths
Currently, I am using a brilliant Nigel Garvey handler to add months
to the older date until it is greater than the newer one, keeping track
of how many month-additions it took to achive, but, I suspect that this
is not the most efficient way to go, (because of my code, not because of
Nigel's handler):
on DifferenceOfMonths(d1, d2)
-- protect dates, (dates are data-sharing objects)
--
copy {d1, d2} to {d1, d2}
-- We add to the older date until it is greater than or
-- equal to the newer date, so ensure that d1 is older:
--
if not (d1 < d2) then set {d1, d2} to {d2, d1}
set x to 0
repeat while (d1 < d2)
set x to x + 1
set d1 to addMonths onto d1 by x
end repeat
return x
end DifferenceOfMonths
(* Nigel Garvey's addMonths()
*
* addMonths onto (current date) by 480 -- add 480 calendar months
* addMonths onto (current date) by -18 -- subtract 18 calendar
months
*)
on addMonths onto theDate by m -- returns a date
copy theDate to d
set {y, m} to {m div 12, m mod 12}
if m < 0 then set {y, m} to {y - 1, m + 12}
set d's year to (d's year) + y
if m is not 0 then tell d to set {day, day} to {32 * m, day}
-- If the day doesn't exist in the target month, return the last day
of that month
if d's day is not theDate's day then -- overflowed into next month
set d's day to 1
set d to d - days
end if
d
end addMonths
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."
Thanks in advance. :)
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.esglabs.com/>
on error number -128
end try
}
_______________________________________________
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.