These are convenience functions to get certain dates relatively to today.

yesterday(ref = today())

tomorrow(ref = today())

week(ref = today())

year(ref = today())

last_week(ref = today(), only_start_end = FALSE)

this_week(ref = today(), only_start_end = FALSE)

next_week(ref = today(), only_start_end = FALSE)

last_month(ref = today(), only_start_end = FALSE)

this_month(ref = today(), only_start_end = FALSE)

next_month(ref = today(), only_start_end = FALSE)

last_quarter(ref = today(), only_start_end = FALSE)

this_quarter(ref = today(), only_start_end = FALSE)

next_quarter(ref = today(), only_start_end = FALSE)

last_year(ref = today(), only_start_end = FALSE)

this_year(ref = today(), only_start_end = FALSE)

next_year(ref = today(), only_start_end = FALSE)

last_n_years(n, ref = end_of_last_year(), only_start_end = FALSE)

last_5_years(ref = end_of_last_year(), only_start_end = FALSE)

last_10_years(ref = end_of_last_year(), only_start_end = FALSE)

start_of_last_week(ref = today(), day = 1)

end_of_last_week(ref = today(), day = 7)

start_of_this_week(ref = today(), day = 1)

end_of_this_week(ref = today(), day = 7)

start_of_last_month(ref = today())

end_of_last_month(ref = today())

start_of_this_month(ref = today())

end_of_this_month(ref = today())

start_of_next_month(ref = today())

end_of_next_month(ref = today())

start_of_last_quarter(ref = today())

end_of_last_quarter(ref = today())

start_of_this_quarter(ref = today())

end_of_this_quarter(ref = today())

start_of_next_quarter(ref = today())

end_of_next_quarter(ref = today())

start_of_last_year(ref = today())

end_of_last_year(ref = today())

start_of_this_year(ref = today())

end_of_this_year(ref = today())

start_of_next_year(ref = today())

end_of_next_year(ref = today())

nth_monday(ref = today(), n = 1)

nth_tuesday(ref = today(), n = 1)

nth_wednesday(ref = today(), n = 1)

nth_thursday(ref = today(), n = 1)

nth_friday(ref = today(), n = 1)

nth_saturday(ref = today(), n = 1)

nth_sunday(ref = today(), n = 1)

week2date(wk, yr = year(today()), day = 1)

week2resp_season(wk, remove_outside_season = FALSE)

Arguments

ref

reference date (defaults to today)

only_start_end

logical to indicate whether only the first and last value of the resulting vector should be returned

n

relative number of weeks

day

day to return (0 are 7 are Sunday, 1 is Monday, etc.)

wk

week to search for

yr

year to search for, defaults to current year

remove_outside_season

a logical to remove week numbers in the range 21-39

Details

All functions return a vector of dates, except for yesterday(), today(), tomorrow(), week2date(), and the start_of_*(), end_of_*() and nth_*() functions; these return 1 date.

Week ranges always start on Mondays and end on Sundays.

year() always returns an integer.

The last_n_years(), last_5_years() and last_10_years() functions have their reference date set to end_of_last_year() at default.

week2resp_season() transforms week numbers to an ordered factor, in a range 40-53, 1:39 (or, if remove_outside_season = TRUE, 40-53, 1:20). This function is useful for plotting.

Examples

today()
#> [1] "2024-11-18"
today() %in% this_month()
#> [1] TRUE

next_week()
#> [1] "2024-11-25" "2024-11-26" "2024-11-27" "2024-11-28" "2024-11-29"
#> [6] "2024-11-30" "2024-12-01"
next_week(only_start_end = TRUE)
#> [1] "2024-11-25" "2024-12-01"

# 2nd Monday of last month:
last_month() |> nth_monday(2)
#> [1] "2024-10-14"

# last_*_years() will have 1 Jan to 31 Dec at default:
last_5_years(only_start_end = TRUE)
#> [1] "2019-01-01" "2023-12-31"
last_5_years(today(), only_start_end = TRUE)
#> [1] "2019-11-19" "2024-11-18"
if (FALSE) { # \dontrun{

  # great for certedb functions:
  certedb::get_diver_data(last_5_years(),
                          Bepaling == "ACBDE")
} # }

df <- data.frame(date = sample(seq.Date(start_of_last_year(),
                                        end_of_this_year(),
                                        by = "day"),
                               size = 500))
df$time <- as.POSIXct(paste(df$date, "12:00:00"))

library(dplyr, warn.conflicts = FALSE)

# these are equal:
df |>
  filter(date |> between(start_of_last_week(),
                          end_of_last_week()))
#>         date                time
#> 1 2024-11-16 2024-11-16 12:00:00
#> 2 2024-11-13 2024-11-13 12:00:00
#> 3 2024-11-11 2024-11-11 12:00:00
#> 4 2024-11-14 2024-11-14 12:00:00
#> 5 2024-11-17 2024-11-17 12:00:00
#> 6 2024-11-12 2024-11-12 12:00:00
df |>
  filter(date %in% last_week())
#>         date                time
#> 1 2024-11-16 2024-11-16 12:00:00
#> 2 2024-11-13 2024-11-13 12:00:00
#> 3 2024-11-11 2024-11-11 12:00:00
#> 4 2024-11-14 2024-11-14 12:00:00
#> 5 2024-11-17 2024-11-17 12:00:00
#> 6 2024-11-12 2024-11-12 12:00:00

# but this does not work:
df |>
  filter(time %in% last_week())
#> [1] date time
#> <0 rows> (or 0-length row.names)

# so be sure to transform times to dates in certain filters
df |>
  filter(as.Date(time) %in% last_week())
#>         date                time
#> 1 2024-11-16 2024-11-16 12:00:00
#> 2 2024-11-13 2024-11-13 12:00:00
#> 3 2024-11-11 2024-11-11 12:00:00
#> 4 2024-11-14 2024-11-14 12:00:00
#> 5 2024-11-17 2024-11-17 12:00:00
#> 6 2024-11-12 2024-11-12 12:00:00