Imagine you have one date and want the first or last day of this date's
month.
For example, you have the date 2018-02-01 and want the last day of its month.
You have to check whether or not its year is a leap year, and this sounds a
tough task.
getdate helps with returning specific dates according to a reference
than can be another date, a month or an year.
getdate(expr, ref, cal = bizdays.options$get("default.calendar"))a vector of dates according to a reference (month or year)
expr represents the day has to be returned, here it follows a few
examples:
"second day"
"10th bizday"
"3rd wed"
"last bizday"
"first fri"
expr is a character string with two terms: "<position> <day>"
positions: first or 1st,
second or 2nd,
third or 3rd, last and XXth (examples
6th or 11th)
days: day, bizday, or weekdays (sun, mon,
tue, wed, thu, fri, sat)
getdate returns dates according to a reference that can be a month or
an year. This reference can be passed as a character vector representing
months or years, or as a numeric vector representing years.
The ISO format must be used to represent years or months with character
vectors.
getdate("10th wed", 2018, "Brazil/ANBIMA")
#> [1] "2018-03-07"
getdate("last bizday", 2010:2018, "Brazil/ANBIMA")
#> [1] "2010-12-31" "2011-12-30" "2012-12-31" "2013-12-31" "2014-12-31"
#> [6] "2015-12-31" "2016-12-30" "2017-12-29" "2018-12-31"
dts <- seq(as.Date("2018-01-01"), as.Date("2018-12-01"), "month")
getdate("first bizday", format(dts, "%Y-%m"), "Brazil/ANBIMA")
#> [1] "2018-01-02" "2018-02-01" "2018-03-01" "2018-04-02" "2018-05-02"
#> [6] "2018-06-01" "2018-07-02" "2018-08-01" "2018-09-03" "2018-10-01"
#> [11] "2018-11-01" "2018-12-03"
getdate("last bizday", Sys.Date(), "Brazil/ANBIMA")
#> [1] "2025-01-07"
getdate("next bizday", Sys.Date(), "Brazil/ANBIMA")
#> [1] "2025-01-09"
getdate("2nd wed", Sys.Date())
#> [1] "2025-01-15"
getdate("next wed", Sys.Date())
#> [1] "2025-01-15"
getdate("last wed", Sys.Date())
#> [1] "2025-01-01"
getdate("next mon", Sys.Date())
#> [1] "2025-01-10"
getdate("last mon", Sys.Date())
#> [1] "2025-01-03"