I have a date in P
date = as.Date("2011-02-23", "%Y-%m-%d")
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")
but "%t" seems to have a different meaning in R. Is there a solution for this problem?
In the Gregorian Calendar, a common year has 365 days.
lubridate
package has required function "days_in_month" with respect to leapyears.
Your example:
date <- as.Date("2011-02-23", "%Y-%m-%d") lubridate::days_in_month(date) # Feb # 28
Leap year (2016):
date_leap <- as.Date("2016-02-23", "%Y-%m-%d") lubridate::days_in_month(date_leap) # Feb # 29
The Hmisc library has a couple of helpful functions for doing this:
require(Hmisc) monthDays(as.Date('2010-01-01'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With