How to convert date to such format

I have a variable and contains date range like "January 01, 2019-December 31, 2019". I want to convert to format "2019Q1-2019Q4". These libraries are installed in RStudio: stringr,lubridate,anytime and chron. How to do this? Thanks in advance!

the_dates <- "January 01, 2019-December 31, 2019"
start <- strsplit(the_dates,"-")[[1]][1]
end <- strsplit(the_dates,"-")[[1]][2]
make_quarter_span <- function(x,y) {
  require(lubridate)
  make_quarter = function(x) {
    convert_date = function(x) mdy(x)
    paste0(year(convert_date(x)),"Q",quarter(convert_date(x)))
  }
  paste0(make_quarter(x),"-",make_quarter(y))
}
make_quarter_span(start,end)
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
#> [1] "2019Q1-2019Q4"
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.