Re: Date question with weeknumber
Re: Date question with weeknumber
- Subject: Re: Date question with weeknumber
- From: "Nigel Garvey" <email@hidden>
- Date: Mon, 3 May 2010 22:53:52 +0100
Bert Groeneveld wrote on Mon, 3 May 2010 09:18:22 +0200:
>Sorry Mark, my fault.
>I was a bit over-enthusiastic to a single person' solution.
>You also gave a lot of input to this topic, as well as some other
>people (Martin, Stan, ES, Michelle).
>Next time I'll be a bit more careful with my comment.
>Thanks everyone.
I don't know. I thought Bert's original reply was quite good. ;) I
expect his enthusiasm was in part due to the fact that my solution was
the only one that did everything he'd asked.
Among the other solutions, I particularly liked Mark's weekdayOnOrBefore
() handler:
on weekdayOnOrBefore(aWeekDay, aDate)
return aDate - days * ((weekday of (aDate - aWeekDay * days)) mod 7)
end weekdayOnOrBefore
… although a greater proportion of integer arithmetic would be slightly
more efficient:
on weekdayOnOrBefore(aWeekDay, aDate)
return aDate - ((7 + (aDate's weekday) - aWeekDay) mod 7) * days
end weekdayOnOrBefore
>> Hello Nigel,
>>
>> Thank you for your answer. This script works really great!
>> … I completely don't understand it!
Here it is again with (hopefully lucid) comments.
-- ISO 8601 says weeks begin on Mondays. Weeks straddling year boundaries
-- belong to the year in which they have the most days. A side-ffect of this
-- is that the first week of any year contains 4th January.
on getDateRange(startWeekNumber)
-- Offsets from 0 will be more useful here than numbers from 1.
set startWeekOffset to startWeekNumber - 1
-- Get a known Monday in the past. All later Mondays are an exact number
-- of weeks after it! In Snow Leopard, calculations using dates before
-- 1583 have been goodness-me'd by a shift to Julian dates before some
-- time in 1582.
set baseMonday to date "Monday 3 January 1583 00:00:00"
set now to (current date)
-- Get a known 4th January.
set Jan4 to date "Tuesday 4 January 1583 00:00:00"
-- Convert it to 4th January this year.
set Jan4's year to now's year
-- The difference between 4th January and the known Monday, modulo weeks,
-- is the difference between 4th January and the Monday of the week in
-- which it occurs, ie. the beginning of the first week of the year.
set yearStart to Jan4 - (Jan4 - baseMonday) mod weeks
-- If the offset of today's week >= the offset of the target week,
-- start at the first week of next year instead.
if ((now - yearStart) div weeks ≥ startWeekOffset) then
set Jan4's year to (Jan4's year) + 1
set yearStart to Jan4 - (Jan4 - baseMonday) mod weeks
end if
-- Get the Monday that begins the target week.
set startWeekStart to yearStart + startWeekOffset * weeks
-- Add 2 days for the Wednesday of that week.
set startWednesday to formatDate(startWeekStart + 2 * days)
-- Add 8 days for the Tuesday of the following week.
set endTuesday to formatDate(startWeekStart + 8 * days)
return {startWednesday, endTuesday}
end getDateRange
on formatDate(theDate)
-- This gets a text already containing any leading zeros.
tell ((theDate's year) * 10000 + (theDate's month as integer) * 100
+ (theDate's day)) as text
return text 7 thru 8 & "-" & text 5 thru 6 & "-" & text 1 thru 4
end tell
end formatDate
getDateRange("03")
--> {"19-01-2011", "25-01-2011"}
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden