Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find next particular day?

Tags:

r

lubridate

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)
like image 721
cho7tom Avatar asked Sep 07 '15 08:09

cho7tom


People also ask

How to get specific day in js?

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.


1 Answers

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.

like image 94
Kirill Avatar answered Sep 30 '22 16:09

Kirill