• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Date question with weeknumber
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Date question with weeknumber


  • Subject: Re: Date question with weeknumber
  • From: "Nigel Garvey" <email@hidden>
  • Date: Sat, 24 Apr 2010 17:14:12 +0100

Bert Groeneveld wrote on Fri, 23 Apr 2010 10:38:01 +0200:

>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.

This would be my take on it:

  -- ISO convention with Monday week starts.
  on getDateRange(startWeekNumber)
    set startWeekOffset to startWeekNumber - 1
    set baseMonday to date "Monday 3 January 1583 00:00:00"

    set now to (current date)
    set Jan4 to date "Tuesday 4 January 1583 00:00:00"
    set Jan4's year to now's year
    set yearStart to Jan4 - (Jan4 - baseMonday) mod weeks

    if ((now - yearStart) div weeks + 1 >= startWeekOffset) then
      set Jan4's year to (Jan4's year) + 1
      set yearStart to Jan4 - (Jan4 - baseMonday) mod weeks
    end if

    set startWeekStart to yearStart + startWeekOffset * weeks
    set startWednesday to formatDate(startWeekStart + 2 * days)
    set endTuesday to formatDate(startWeekStart + 8 * days)

    return {startWednesday, endTuesday}
  end getDateRange

  on formatDate(theDate)
    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

  • Follow-Ups:
    • Re: Date question with weeknumber
      • From: "Nigel Garvey" <email@hidden>
  • Prev by Date: Re: Removing a System Preferences panel via applescript
  • Next by Date: Re: Date question with weeknumber
  • Previous by thread: Re: Date question with weeknumber
  • Next by thread: Re: Date question with weeknumber
  • Index(es):
    • Date
    • Thread