I need to find all 'next friday' corresponding to a set of dates.
For instance 2015-08-03 (Monday 3rd, August, 2015) as an input should return 2015-08-07 (Friday 7th, August, 2015) as an output.
I could not find a way to manage this need while reading lubridate's vignette, how would you proceed?
library(lubridate)
date <- "2015-08-03"
date <- wmd(date)
wday(date, label = TRUE)
Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Try this function:
nextweekday <- function(date, wday) {
date <- as.Date(date)
diff <- wday - wday(date)
if( diff < 0 )
diff <- diff + 7
return(date + diff)
}
You insert your date and the desired wday (Sunday = 1, Monday = 2, ...) and get the result you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With