找出R中一个月的天数

问题描述:

我有一个日期在P

 date = as.Date("2011-02-23", "%Y-%m-%d")

是否可以找出当月的天数那个特定日期? (关于leapyears)。在PHP中,它看起来与此类似( http://www.php.net/ manual / en / function.date.php ):

Is it possible to find out the number of days of the month of that particular date? (With respect to leapyears). In PHP it would look similar to this (http://www.php.net/manual/en/function.date.php):

days = format(date, "%t")

但%t似乎在R中有不同的含义。有没有解决这个问题?

but "%t" seems to have a different meaning in R. Is there a solution for this problem?

您可以编写简单的功能:

You can write simple function to do that:

numberOfDays <- function(date) {
    m <- format(date, format="%m")

    while (format(date, format="%m") == m) {
        date <- date + 1
    }

    return(as.integer(format(date - 1, format="%d")))
}

调用为:

> date = as.Date("2011-02-23", "%Y-%m-%d")
> numberOfDays(date)
[1] 28
> date # date is unchanged
[1] "2011-02-23"