Re: Date question with weeknumber
Re: Date question with weeknumber
- Subject: Re: Date question with weeknumber
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 23 Apr 2010 08:17:53 -0400
The first question is, how are you counting weeks? Do they start on Sunday or Monday, and when is week 1 of the year? There are at least three common options for the latter - first week containing any part of the new year, first week entirely in the new year, or first week at least halfway into the new year (the ISO definition).
Once you know that, it's not hard to write the necessary function:
-- uncomment one of these, or devise your own - first day of week followed by
-- the date that defines week 1 of the year.
--property first_week: { monday, 4, january } -- ISO 8601
--property first_week: { sunday, 1, january } -- US conventional
--property first_week: { sunday, 7, january } -- US, first full week of year
-- Given a weekday and a date, return the date on which the last
-- such weekday on or before that date falls.
on weekdayOnOrBefore(aWeekDay, aDate)
return aDate - days * ((weekday of (aDate - aWeekDay * days)) mod 7)
end
-- Given a year, a week number, and a weekday, return the date
-- of that weekday in that week. No range checks - asking for week
-- 54 of the year will return the 1st or 2nd week of next year.
on dateFromYWD(year, week, day)
set keyDate to current date
set year of keyDate to year
tell keyDate to set {its day, its month} to items 2 thru 3 of first_week
set week1 to weekdayOnOrBefore(item 1 of first_week, keyDate)
return weekdayOnOrBefore(day, week1 + week * weeks - 1*days)
end
set firstDay to dateFromYWD(someYear, myWeek, wednesday)
set lastDay to dateFromYWD(someYear, myWeek+1, tuesday)
On Fri, Apr 23, 2010 at 4:38 AM, Bert Groeneveld
<email@hidden> wrote:
Hello,
At some point in my script I know a weeknumber, for example set myWeeknumber to "03", or set myWeeknumber to "52" (can be any weeknumber in the future).
I want to find ou the date of the wednesday that falls in the week with weeknumber myWeeknumber (this will be my startDate).
And I want to find out the date of the tuesday that falls in the week after the week with weeknumber myWeeknumber (this will be my endDate).
The format of the startDate end endDate should be "23-04-2010" (23th of april of 2010).
myWeeknumber is always a weeknumber in the future so when today falls in week 52 of a given (this) year and myWeeknumber is set to "2" or "02" then the startDate end the endDate fall in the next year of course.
I was wondering if this can be done with Applescript and if yes, anyone would be so kind to post example code.
Thanks for any help,
Bert.
_______________________________________________
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
--
Mark J. Reed <
email@hidden>
_______________________________________________
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