Creating Calendars

2022-06-22

Creating a calendars

Use create.calendar with your list of holidays.

create.calendar(
  name = "MyCalendar", holidays = holidays, weekdays = c("sunday", "saturday"),
  adjust.from = adjust.next, adjust.to = adjust.previous
)

what should be considered:

Now you can call bizdays functions passing the calendar name.

is.bizday("2016-07-12", "MyCalendar")
## [1] TRUE
following("2016-09-07", "MyCalendar")
## [1] "2016-09-08"
bizdays("2016-07-12", "2016-10-16", "MyCalendar")
## [1] 66

Of course you can assign the calendar to a variable directly and pass this variable to bizdays functions

cal <- create.calendar(
  name = "MyCalendar", holidays = holidays, weekdays = c("sunday", "saturday"),
  adjust.from = adjust.next, adjust.to = adjust.previous
)
is.bizday("2016-07-12", cal)
## [1] TRUE

But this is not expected to work that way.

Why define weekdays?

I am frequently asked Why do I have to define weekdays? or even Shouldn’t it be weekenddays instead?.

The reason I created weekdays: I want to provide a way to compute business days accordingly to any definition or satisfying any needs. In my world, the financial industry, weekends are nonworking days, but for those who work with events, for example, mondays migth be nonworking days.

weekdays defaults to NULL because I wanted the Calendar() call returned an Actual calendar.

Skiping weekends only (defining weekends as nonworking days)

You can define whatever calendar you want, for example, a calendar without holidays where only weekdays are nonworking days.

create.calendar(name = "WeekendsOnly", weekdays = c("sunday", "saturday"))

define only weekdays to weekend days.

from_dates <- "2013-01-01"
to_dates <- seq(as.Date("2013-12-31"), as.Date("2020-12-31"), by = "years")
bizdays(from_dates, to_dates, "WeekendsOnly")
## [1]  260  521  782 1043 1303 1564 1825 2087

Skiping mondays (defining mondays as nonworking days)

create.calendar(name = "EveryMonday", weekdays = "monday")
from_dates <- "2013-01-01"
to_dates <- seq(as.Date("2013-12-31"), as.Date("2020-12-31"), by = "years")
bizdays(from_dates, to_dates, "EveryMonday")
## [1]  312  625  938 1252 1565 1877 2190 2504

Skip nothing (formal current days calendar)

create.calendar(name = "Actual")
from_dates <- "2013-01-01"
to_dates <- seq(as.Date("2013-12-31"), as.Date("2020-12-31"), by = "years")
bizdays(from_dates, to_dates, "Actual")
## [1]  364  729 1094 1460 1825 2190 2555 2921