How to find difference between two dates excluding Sunday in R

306 views Asked by At

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?

1

There are 1 answers

1
www On

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