R/time_functions.R

#' @import lubridate

#' @export
last_month <- function(now = Sys.Date()) {
  month(now) <- month(now) - 1
  day(now) <- 1
  return(now)
}

#' @export
last_week <- function(now = Sys.Date()) {
  return(now - dweeks())
}

#' @export
elapsed_months <- function(from, to = Sys.Date(), absolute = TRUE) {
  if (is.character(from))
    from <- as.Date(from)
  if (is.character(to))
    to <- as.Date(to)
  nmonths <- 12 * (year(to) - year(from)) + (month(to) - month(from))
  return(ifelse(absolute, abs(nmonths), nmonths))
}
domjarkey/crunchscripts documentation built on June 6, 2019, 7:43 p.m.