[R] finding the last day of the month
Ben Bolker
bbolker at gmail.com
Mon Nov 8 13:40:26 CET 2010
Benjamin Williams <ben <at> bwmcct.com> writes:
> I am trying to get fields showing the last day of each month for a monthly
> closing project. In order to find the last day of the previous month, I
> subtract the number of days from the current month. For all months my code
> works; however, for October, my code doesn't work...it returns
> 2010-09-*29* instead
> of 2010-09-*30*.
>
> format(strptime("2010-10-31",
> "%Y-%m-%d")-(as.numeric(format(as.Date("2010-10-31"),"%d"))*24*3600),
"%Y-%m-%d")
Very timely question.
This is a daylight savings time issue; subtracting 24 hours
gives a different answer when there are only 23 hours in a particular
day ("spring forward, fall back"). Search the R-help
archives for the last week or so for a similar issue.
(Hint: try subtracting days from dates rather than converting
to seconds first.)
Ben Bolker
More information about the R-help
mailing list