Any idea how to get the date of every Thursday of a month, year, year range etc?
I’m writing a script that is going to manage a bunch of digital asset metadata and then import it into Extensis Portfolio. Our newspaper prints every Thursday so it’d be useful to have a list of dates we printed on.
getThursdate("1/1/2004", "1/31/2005")
on getThursdate(startdate, enddate)
--convert input arguments to dates
set sd to date startdate
set ed to date enddate
-- initialize output list
set theList to {}
--find nearest Thursday to start date
repeat while (weekday of sd) as string is not "Thursday"
set sd to ((sd) + 1 * days)
end repeat
--get every subsequent Thursday and append it to theList
--Note: sure, you could just iterate over every day and grab each thursday, but this is faster! :-)
repeat while (sd < ed)
set sd to ((sd) + 1 * weeks)
set end of theList to sd
end repeat
return theList
end getThursdate
Here is another version, based on the fact that it should exist a thursday every 7 days:
getThursdate("1/27/2005", "1/27/2038") --> you must provide in the first parameter the FIRST thursday you want included in your list
on getThursdate(startThursday, enddate)
--convert input arguments to dates
set startThursday to date startThursday
set enddate to date enddate
set thursdayLst to {startThursday}
repeat with i from 7 to (round ((enddate - startThursday) / 86400)) by 7
set thursdayLst's end to startThursday + (i * days)
end repeat
thursdayLst
end getThursdate
Hi. This repeat needs a couple of small adjustments. Otherwise it’s one week ahead all the way through the list:
repeat until (sd > ed) -- ie. while (sd <= ed)
set end of theList to sd
set sd to ((sd) + 1 * weeks)
end repeat
You can, of course, quickly get the first Thursday after (or including) a start date by rounding up mathematically. (Or by adding six days and rounding down.)
tell startdate + 6 * days to ¬
set startThursday to it - (it - date "Thursday, 2 January 1000 00:00:00") mod weeks