MattJayC

Q: Reverse look up week number.

I had some help with this in the past and  I had someone help create a week number based around my work's system.

 

If I know the week number can I use it to look up the week commencing date?

 

So for example we are in week 1 (Wednesday, 24 August 2016 at 13:33:27) If I input week 1 , can I make set theStart of theweek to "Monday, 22 August 2016 at 00:00:00"?

 

set thisDate to current date


set theweekNumber to ((thisDate - (my augustThirdWeek(thisDate))) div 604800)


on augustThirdWeek(d) -- get the third monday of August
  copy d to tDate
  tell tDate
  set {its day, its month, its time} to {15, 8, 0} -- start at August 15
  repeat 2 times
  repeat while (its weekday) is not Monday
  set its day to (its day) + 1 -- add one day (range 16 ... 21)
  end repeat
  if d < it then -- this date is older that the third Monday of August
  set {its day, its year} to {15, (its year) - 1} -- subtract one year
  else
  return it
  end if
  end repeat
  end tell
end augustThirdWeek

iMac, OS X Mavericks (10.9.4)

Posted on Aug 24, 2016 5:57 AM

Close

Q: Reverse look up week number.

  • All replies
  • Helpful answers

  • by Niel,

    Niel Niel Aug 24, 2016 6:25 AM in response to MattJayC
    Level 10 (311,758 points)
    Aug 24, 2016 6:25 AM in response to MattJayC

    Use the following:

     

    set the_date to date "Wednesday, August 24, 2016 at 13:33:27"

    set {hours of the_date, minutes of the_date, seconds of the_date} to {0, 0, 0}

    set the_date to the_date - (((day of the_date) - Sunday) * 3600 * 24)

     

    (144218)

  • by MattJayC,

    MattJayC MattJayC Aug 24, 2016 6:29 AM in response to Niel
    Level 1 (5 points)
    Mac OS X
    Aug 24, 2016 6:29 AM in response to Niel

    Sorry I mean I only know the week number.

    Therefore I need input 1 (or whatever week number it is to then output out the first Monday of that week.

  • by VikingOSX,

    VikingOSX VikingOSX Aug 24, 2016 8:09 AM in response to MattJayC
    Level 7 (20,591 points)
    Mac OS X
    Aug 24, 2016 8:09 AM in response to MattJayC

    [redacted - returns wrong Monday /viking]

  • by MattJayC,

    MattJayC MattJayC Aug 24, 2016 8:43 AM in response to VikingOSX
    Level 1 (5 points)
    Mac OS X
    Aug 24, 2016 8:43 AM in response to VikingOSX

    as above

  • by Jacques Rioux,Solvedanswer

    Jacques Rioux Jacques Rioux Aug 24, 2016 11:03 AM in response to MattJayC
    Level 4 (3,408 points)
    Mac OS X
    Aug 24, 2016 11:03 AM in response to MattJayC

    Hi,

     

    if you want a date like ("Monday, 22 August 2016 00:00:00" for the  first week,  "Monday, 29 August 2016 00:00:00" for the second week, and so on):

    Try this script (work when Monday is the first day of the week):

    ---------------

    set thisMonday to my mondayFromThisWeekNumber(1)

     

    on mondayFromThisWeekNumber(weekNum)

        tell (current date)

              set {its day, its month, its time} to {15, 8, 0} -- start at August 15

              set tDate to it + (weekNum * weeks) -- add weeks

        end tell

        tell tDate

              repeat while (its weekday) is not Monday

                  set its day to (its day) - 1 -- subtract one day

              end repeat

              return it

        end tell

    end mondayFromThisWeekNumber

    ---------------

  • by Hiroto,

    Hiroto Hiroto Aug 24, 2016 11:28 AM in response to MattJayC
    Level 5 (7,276 points)
    Aug 24, 2016 11:28 AM in response to MattJayC

    Hello

     

    You may try something like the following.

     

    Both calculation of week number for a day and calculation of monday for a week number are done by common months_nth_monday() handler.

     

    Your original augustThirdWeek() handler can be rewritten using months_nth_monday() handler as shown in comment.

     

     

    --return months_nth_monday(2016, 8, 3) -- test
    
    set this_day to current date
    --set this_week_number to (this_day - augustThirdWeek(this_day)) div (7 * days)
    set this_week_number to (this_day - months_nth_monday(2016, 8, 3)) div (7 * days)
    set this_monday to months_nth_monday(2016, 8, this_week_number + 3)
    
    return {this_day, this_week_number, this_monday}
    
    on months_nth_monday(y, m, n)
        (*
            integer y, m : year and month
            integer n : ordinal number n for n'th Monday
            return date : The n'th Monday in month m of year y
        *)
        tell (current date)
            set {its day, its year, its month, its time, t} to {1, y, m, 0, it} -- y-m-1
        end tell
        set w1 to (5 + (t's weekday as integer)) mod 7 -- weekday of y-m-1 : monday = 0, sunday = 6
        set m1 to t + (7 - w1) mod 7 * days -- 1st monday
        return m1 + 7 * (n - 1) * days -- n'th monday
    end months_nth_monday
    
    (*
    on augustThirdWeek(d)
        months_nth_monday(d's year, 8, 3)
    end augustThirdWeek
    *)
    

     

     

     

     

    Regards,

    H

     

    EDIT. fixed comment in script

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux Aug 24, 2016 1:00 PM in response to Jacques Rioux
    Level 4 (3,408 points)
    Mac OS X
    Aug 24, 2016 1:00 PM in response to Jacques Rioux

    My script is doesn't work when the current date is between  January 1 and  August 14

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux Aug 24, 2016 1:19 PM in response to Jacques Rioux
    Level 4 (3,408 points)
    Mac OS X
    Aug 24, 2016 1:19 PM in response to Jacques Rioux

    Fixed the script, I forgot to subtract one year when the current date is less than  August 15

     

    -----

    set thisMonday to my mondayFromThisWeekNumber(1)

    on mondayFromThisWeekNumber(weekNum)

         set cDate to current date

         copy cDate to tDate

         tell tDate to set {its day, its month, its time} to {15, 8, 0} -- start at August 15

         if cDate < tDate then tell tDate to set its year to (its year) - 1 -- subtract one year, because the current date  is between  January 1 and  August 14

         tell tDate + (weekNum * weeks) -- add weeks

              repeat while (its weekday) is not Monday

                   set its day to (its day) - 1 -- subtract one day

              end repeat

              return it

         end tell

    end mondayFromThisWeekNumber

  • by VikingOSX,

    VikingOSX VikingOSX Aug 25, 2016 4:53 AM in response to MattJayC
    Level 7 (20,591 points)
    Mac OS X
    Aug 25, 2016 4:53 AM in response to MattJayC

    The following AppleScript/Python script given "2016 34" will return Monday 22 August 2016 at 00:00:00. Tested on OS X 10.11.6 and using AppleScript 2.5, and standard OS X System Python 2.7.10.

     

    -- Given an argument string comprised of a year, and week of the year,

    -- return first monday of specified week formatted as:

    -- "2016 34" returns Monday, 22 August 2016 at 00:00:00

     

    set args to "2016 34"

    display dialog start_of_week(args) as text

    return

     

    on start_of_week(args)

         return do shell script "python <<EOF - " & args & "

    #/usr/bin/python

    # coding: utf-8

     

    # uses minkwe function from:

    # http://stackoverflow.com/questions/396913/in-python-how-do-i-find-the-date-of-th e-first-monday-of-a-given-week

     

    from datetime import date, timedelta

    import sys

     

    def first_monday(ayear, aweek):

         d = date(ayear, 1, 4)  # ISO stipulation that 4th Jan always in week 1

         return d + timedelta(weeks=(aweek-1), days=-d.weekday())

     

    year = int(sys.argv[1])

    week = int(sys.argv[2])

    if year and week:

         # reverse %B and %e for U.S. AppleScript date compatibility. See man strftime(3).

         print(\"{}\".format(first_monday(year, week).strftime(\"%A, %e %B %Y at %T\")))

    EOF"

    end start_of_week