Returns the most recent date that falls on a specific day of the week. Useful for finding dates like "last Friday" for file naming when databases refresh on specific days.
Usage
last_weekday(weekday, as_of = Sys.Date(), include_today = TRUE)Examples
# Find last Friday
last_weekday("Friday")
#> [1] "2026-02-06"
# Using abbreviation
last_weekday("fri")
#> [1] "2026-02-06"
# Using number (6 = Friday)
last_weekday(6)
#> [1] "2026-02-06"
# From a specific date
last_weekday("Friday", as_of = as.Date("2024-06-15"))
#> [1] "2024-06-14"
#> "2024-06-14"
# Exclude today even if it matches
last_weekday("Friday", as_of = as.Date("2024-06-14"),
include_today = FALSE)
#> [1] "2024-06-07"
#> "2024-06-07"