days_around_today: Dates around Today

days_around_todayR Documentation

Dates around Today

Description

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

Usage

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)

last_n_months(n, ref = end_of_last_month(), only_start_end = FALSE)

last_3_months(ref = end_of_last_month(), only_start_end = FALSE)

last_6_months(ref = end_of_last_month(), only_start_end = FALSE)

last_n_weeks(n, ref = end_of_last_week(), only_start_end = FALSE)

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

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

start_of_last_week(ref = today())

end_of_last_week(ref = today())

start_of_this_week(ref = today())

end_of_this_week(ref = today())

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

wk

week to search for

yr

year to search for, defaults to current year

day

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

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.

The last_n_months(), last_3_months() and last_6_months() functions have their reference date set to end_of_last_month() at default.

The last_n_weeks() function has its reference date set to end_of_last_week() 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()
today() %in% this_month()

next_week()
next_week(only_start_end = TRUE)

# 2nd Monday of last month:
last_month() |> nth_monday(2)

# last_*_years() will have 1 Jan to 31 Dec at default:
last_5_years(only_start_end = TRUE)
last_5_years(today(), only_start_end = TRUE)

last_3_months(only_start_end = TRUE)

year_to_date(only_start_end = TRUE)

## Not run: 

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

## End(Not run)

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()))
df |>
  filter(date %in% last_week())

# but this does not work:
df |>
  filter(time %in% last_week())

# so be sure to transform times to dates in certain filters
df |>
  filter(as.Date(time) %in% last_week())

certe-medical-epidemiology/certetoolbox documentation built on April 17, 2025, 3:24 a.m.