[R] How to get last day of a month?

Alex Brussee alex.brussee at gmail.com
Tue Dec 8 08:19:28 CET 2009


To get the last day of the month, I used the following statement:

as.Date(format(as.Date(format(as.Date("20090323","%Y%m%d"), "%Y%m01"),
"%Y%m%d")+31,"%Y%m01"), "%Y%m%d")-1

Of course, "20090323" is the original yyyymmdd input date here. It's not
very sophisticated, but gets the job done in a simple way


Diethelm Wuertz wrote:
> 
> jim holtman wrote:
> 
> just use timeDate from Rmetrics, there you will find several functions 
> like ...
> 
> #  timeLastDayInMonth        Computes the last day in a given month and
> year
> #  timeFirstDayInMonth       Computes the first day in a given month and 
> year
> #  timeLastDayInQuarter      Computes the last day in a given quarter 
> and year
> #  timeFirstDayInQuarter     Computes the first day in a given quarter 
> and year
> 
> #  timeNthNdayInMonth        Computes n-th ocurrance of a n-day in 
> year/month
> #  timeLastNdayInMonth       Computes the last n-day in year/month
> 
> and many more ...
> 
> Diethelm Wuertz
> 
> 
>> Does this help.  Shows how to use the basic functions to get at the
>> answer:
>>
>>   
>>> # add first day of month to make it valid
>>> x <- as.POSIXct(paste('1', 'sep2009', sep=''), format="%d%b%Y")
>>> # now advance one month and then go back one day for the end of the
>>> month
>>> next.mon <- seq(x, length=2, by='1 month')[2]
>>> last.day <- seq(next.mon, length=2, by='-1 day')[2]
>>> last.day
>>>     
>> [1] "2009-09-30 GMT"
>>   
>>
>>
>> On Sun, Sep 13, 2009 at 2:18 PM, megh <megh700004 at yahoo.com> wrote:
>>   
>>> Is there any R function to calculate automatically the last day of a
>>> particular month? For example "sep2009" should be converted to last day
>>> of
>>> September of 2009?
>>>
>>> Thanks
>>> --
>>> View this message in context:
>>> http://www.nabble.com/How-to-get-last-day-of-a-month--tp25425645p25425645.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>     
>>
>>
>>
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://n4.nabble.com/How-to-get-last-day-of-a-month-tp890694p955034.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list