0

I have two dates and I want to find no. of days between these dates excluding Sunday. I tried it using bizdays() but it doesn't seem to be giving correct output.

Here is what I've tried.

library(bizdays)

#Dates are in Y-m-d format

Start_Date <- "2017-11-21"

End_Date <- "2017-12-03"

create.calendar("FOSCal", holidays = integer(0), weekdays = c("sunday"),
            start.date = Start_Date, end.date = End_Date, adjust.from = adjust.none,
            adjust.to = adjust.none)

FOS_Days <- as.integer(bizdays(Start_Date, End_Date, "FOSCal"))

This code is giving me output as 10 whereas it should give 11. I think it has something to do with EndDate because it is Sunday but not sure. Could anyone please let me know what I'm missing here?

www
  • 38,575
  • 12
  • 48
  • 84
Kailash
  • 167
  • 1
  • 2
  • 12
  • I just tested at `as.integer(bizdays("2017-11-21", "2017-11-22", "FOSCal"))`, which returns `1`. and `as.integer(bizdays("2017-11-21", "2017-11-23", "FOSCal"))`, which returns `2`. I think by adding `1` you can have the desired output. It is just a matter of definition on how to count days (should the beginning dates or ending dates included?) – www Dec 28 '17 at 05:54
  • Yes start and end date should be included. – Kailash Dec 28 '17 at 06:21
  • And I guess you just add 1 to all outputs. – www Dec 28 '17 at 06:29

1 Answers1

0

We can create a sequence based on the Start_Date and End_Date, use `weekdays function to get weekdays, and then exclude Sunday and count the length of the sequence. The following code shows the answer is 11.

# Create starting and ending dates
Start_Date <- "2017-11-21"
End_Date <- "2017-12-03"

# Create a date sequence
date_seq <- seq.Date(from = as.Date(Start_Date),
                     to = as.Date(End_Date),
                     by = 1)

# Get the weekday
weekday_seq <- weekdays(date_seq)

# Exclude Sunday and count the length
length(weekday_seq[!weekday_seq %in% "Sunday"])
# [1] 11
www
  • 38,575
  • 12
  • 48
  • 84
  • Thanks a lot. However I wanted to know if it can be done using bizdays coz I may need to exclude few holidays as well in future other than Sunday. In create.calender we've got an option to define holidays. Can we exclude list of holidays using this method too? – Kailash Dec 28 '17 at 05:39