Last Friday of each month: Difference between revisions
Content added Content deleted
Line 178: | Line 178: | ||
=={{Header|AppleScript}}== |
=={{Header|AppleScript}}== |
||
AppleScript weekday constants can be coerced either to English text or to the integers 1 (for Sunday) to 7 (Saturday). |
|||
<lang AppleScript>on lastFridayOfEachMonthInYear(y) |
|||
-- Initialise an AppleScript date to the first day of some month in the specified year. |
|||
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
|||
-- Get a string representation of y, zero-padded if necessary, and initialise the output string. |
|||
set y to text 2 thru 5 of ((10000 + y) as text) |
|||
set outputText to "./last_fridays " & y |
|||
repeat with nextMonth from 2 to 13 -- Yes! |
|||
-- For each month in the year, get the first day of the following month. |
|||
set firstDayOfNextMonth's month to nextMonth |
|||
-- Calculate the date of the Friday which occurs in the seven days prior to that |
|||
-- by subtracting a day plus the difference between the previous day's weekday and the target weekday. |
|||
set lastFridayOfThisMonth to firstDayOfNextMonth - (1 + (firstDayOfNextMonth's weekday) mod 7) * days |
|||
-- Append the required details to the output text. |
|||
set {month:m, day:d} to lastFridayOfThisMonth |
|||
tell (10000 + m * 100 + d) as text to ¬ |
|||
set outputText to outputText & (linefeed & y & "-" & text 2 thru 3 & "-" & text 4 thru 5) |
|||
end repeat |
|||
return outputText |
|||
end lastFridayOfEachMonthInYear |
|||
lastFridayOfEachMonthInYear(2020)</lang> |
|||
{{Out}} |
|||
<pre>"./last_fridays 2020 |
|||
2020-01-31 |
|||
2020-02-28 |
|||
2020-03-27 |
|||
2020-04-24 |
|||
2020-05-29 |
|||
2020-06-26 |
|||
2020-07-31 |
|||
2020-08-28 |
|||
2020-09-25 |
|||
2020-10-30 |
|||
2020-11-27 |
|||
2020-12-25"</pre> |
|||
The above is of course hard-coded for Fridays. It can be made more flexible by taking an AppleScript weekday constant as a parameter: |
|||
<lang AppleScript>on lastWeekdayWOfEachMonthInYear(w, y) -- Parameters: (AppleScript weekday constant, AD year number) |
|||
-- Initialise an AppleScript date to the first day of some month in the specified year. |
|||
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
|||
-- Get a string representation of y, zero-padded if necessary, and initialise the output string. |
|||
set y to text 2 thru 5 of ((10000 + y) as text) |
|||
set outputText to "./last_" & w & "s " & y |
|||
repeat with nextMonth from 2 to 13 -- Yes! |
|||
-- For each month in the year, get the first day of the following month. |
|||
set firstDayOfNextMonth's month to nextMonth |
|||
-- Calculate the date of the target weekday which occurs in the seven days prior to that. |
|||
-- The calculation can be described in various ways, the simplest being the subtraction of a day plus the difference between the previous day's weekday and the target weekday: |
|||
-- firstDayOfNextMonth - (1 + (((firstDayOfNextMonth's weekday) - 1) - w + 7) mod 7) * days |
|||
-- But they all boil down to: |
|||
set lastWOfThisMonth to firstDayOfNextMonth - (1 + ((firstDayOfNextMonth's weekday) - w + 6) mod 7) * days |
|||
-- Get the day and month of the calculated date and append the required details to the output text. |
|||
set {month:m, day:d} to lastWOfThisMonth |
|||
tell (10000 + m * 100 + d) as text to ¬ |
|||
set outputText to outputText & (linefeed & y & "-" & text 2 thru 3 & "-" & text 4 thru 5) |
|||
end repeat |
|||
return outputText |
|||
end lastWeekdayWOfEachMonthInYear |
|||
lastWeekdayWOfEachMonthInYear(Friday, 2020)</lang> |
|||
{{Out}} |
|||
<pre>"./last_Fridays 2020 |
|||
2020-01-31 |
|||
2020-02-28 |
|||
2020-03-27 |
|||
2020-04-24 |
|||
2020-05-29 |
|||
2020-06-26 |
|||
2020-07-31 |
|||
2020-08-28 |
|||
2020-09-25 |
|||
2020-10-30 |
|||
2020-11-27 |
|||
2020-12-25"</pre> |
|||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
<lang AppleScript>-- LAST FRIDAYS OF YEAR ------------------------------------------------------ |
<lang AppleScript>-- LAST FRIDAYS OF YEAR ------------------------------------------------------ |