Skip to content

Instantly share code, notes, and snippets.

@MarkEdmondson1234
Created March 9, 2018 10:56
Show Gist options
  • Save MarkEdmondson1234/a1a8198e658b8f7700f0ec7f2830bdc4 to your computer and use it in GitHub Desktop.
Save MarkEdmondson1234/a1a8198e658b8f7700f0ec7f2830bdc4 to your computer and use it in GitHub Desktop.
Create month start and end dates for a date range you pass in
add_months <- function(date, n){
seq(date, by = paste (n, "months"), length = 2)[2]
}
make_date_ranges <- function(start, end){
starts <- seq(from = start,
to = Sys.Date()-1 ,
by = "1 month")
ends <- c((seq(from = add_months(start, 1),
to = end,
by = "1 month" ))-1,
(Sys.Date()-1))
data.frame(starts,ends)
}
## useage
make_date_ranges(as.Date("2017-01-01"), Sys.Date())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment