How to get week ending next Sunday from date

I know it lubridate

has a function ceiling_date

, but it provides a week ending next Saturday from a specific date. How can I change it to get the week ending next Sunday instead?

> ceiling_date(as.Date('2017-06-16'), 'week')
[1] "2017-06-17 20:00:00 EDT"

      

+3


source to share


1 answer


I think it might be a bug with ceiling_date

as pointed out here: https://github.com/tidyverse/lubridate/issues/479

One workaround might be to grab the year / week and then round up to Sunday:

as.Date(paste0(year('2017-06-16'), week('2017-06-16') + ifelse(wday('2017-06-16')==1,0,1), 7), format =  "%Y%U%u")
#[1] "2017-06-18"

      



From ?strptime

:

  • %Y

    - year with century (i.e. 2017).
  • %U

    is the week of the year as decimal
  • %U

    - day of the week from 1-7 (1 - Monday)

So we need to shift our week by 1

(hence +1

) and then we will set the day to Sunday 7

.

0


source







All Articles