Re: Date of next Thursday
Re: Date of next Thursday
- Subject: Re: Date of next Thursday
- From: Nigel Garvey <email@hidden>
- Date: Thu, 16 Nov 2000 02:16:04 +0000
Michelle Steiner wrote on Wed, 15 Nov 2000 15:42:03 -0800:
>
On 11/15/00 3:17 PM, Nigel Garvey <email@hidden> wrote
>
>
>set dref to date "Thursday, 6 January 2000 00:00:00"
>
>set nextThursday to dref + (round (((current date) - dref) / weeks)
>
>rounding up) * weeks
>
>
Wonderful! May I add a very small enhancement?
>
>
set nextThursday to date string of (dref + (round ((((current date) + 47
>
* days) - dref) / weeks) rounding up) * weeks)
>
>
This removes the time from the result, leaving only the date.
Oh right. Yes, of course. In fact the formula could be made slightly more
efficient, so to further enhance your enhancement (assuming that the
extra 47 days was left over from an experiment):
set dref to date "Thursday, 6 January 2000 00:00:00"
set nextThursday to date string of ,
(dref + ((current date) + weeks - dref) div weeks * weeks)
If one wanted instead the first day of next month, a short way would be:
set {d, day of d, day of d} to {current date, 32, 1}
d
Similarly, if you wanted to know the number of days in the current month:
set {d, day of d, day of d} to {current date, 32, 1}
day of (d - days)
... or in the style of Richard 23 ;-)
tell (current date)
set {day, day} to {32, 1}
(it - days)'s day
end tell
And finally, to exhaust my stock of rarely useful date tricks, a handler
for performing month arithmetic:
on addMonths(theDate, m)
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 year of d to (year of d) + y
if m is not 0 then set {day of d, day of d} to {32 * m, day of d}
-- If the day doesn't exist in the target month
-- return the last day of that month
if day of d is not day of theDate then -- overflowed into next month
set day of d to 1
set d to d - days
end if
d
end addMonths
addMonths(current date, -240)
NG