Rebol3 Code Examplex


Nth Particular Weekday of the Month

Compute the date of the n-th occurrence of a given weekday in a specified month/year (e.g., 2nd Monday of October).

Rebol [
    title: "Rosetta code: Nth Particular Weekday of the Month"
    file:  %Nth_Particular_Weekday_of_the_Month.r3
    url:   https://rosettacode.org/wiki/Nth_Particular_Weekday_of_the_Month
]

nth-weekday: function [
    "Return the date of the nth occurrence of weekday in month/year, or none"
    year [integer!] month [integer!] weekday [integer!] n [integer!]
][
    date: to date! reduce [year month 1]
    while [date/month == month] [
        if all [
            date/weekday == weekday
            n == (1 + to integer! (date/day - 1) / 7)
        ][
            return date/day
        ]
        date: date + 1
    ]
    none
]

print "    Thanksgiving Day"
print "Year Canadian   American"
print "------------------------"
for year 2020 2029 1 [
    canadian: nth-weekday year 10 1 2  ;; 2nd Monday of October
    american: nth-weekday year 11 4 4  ;; 4th Thursday of November
    printf [4 "  Oct " 2 "    Nov " 2] [year canadian american]
]

Output:

    Thanksgiving Day
Year Canadian   American
------------------------
2020  Oct 12    Nov 26
2021  Oct 11    Nov 25
2022  Oct 10    Nov 24
2023  Oct 9     Nov 23
2024  Oct 14    Nov 28
2025  Oct 13    Nov 27
2026  Oct 12    Nov 26
2027  Oct 11    Nov 25
2028  Oct 9     Nov 23
2029  Oct 8     Nov 22