# [R-SIG-Finance] Relative Date Question

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 28 15:00:03 CEST 2008

```Here are a couple of possibilities.

library(zoo) # as.yearmon, as.Date.yearmon

# test data
x <- seq(as.Date("2000-02-01"), by = "month", length = 6)-1
x

# use same number of days past start of next month
ans1 <- as.Date(as.yearmon(x) + 1/12) + as.numeric(format(x, "%d")) - 1
ans1

# or if you don't want to go beyond the end of the next month:
pmin(ans1, as.Date(as.yearmon(x) + 2/12)-1)

On Thu, Aug 28, 2008 at 8:02 AM, Peter Carl <peter at braverock.com> wrote:
> On Thursday 28 August 2008 4:14:58 am Ruvashen Padayachee wrote:
>> For example, how can I find the date one month after "2008/09/30" ?
>
> The class 'Date' provides a 'seq.Date' function for creating date sequences.
>> seq(as.Date("2008-09-01"),length.out=12, by="1 month")
>> seq(ISOdate(2008,9,30), by = "month", length = 4)
>
> Note that this second example, which attempts to create a sequence of
> end-of-month dates, doesn't work very well.  How to find last day of the
> month in a date sequence?  Unfortunately, the solution is so trivial that it
> might not be obvious.
>
>> seq(as.Date("2008-9-01"),length.out=4, by="1 month")-1
>
> HTH
>
> pcc
> --
> Peter Carl
> 145 Scottswood Rd
> Riverside, IL 60546
> 312 307 6346
> http://www.braverock.com/~peter
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>

```