Find Out Number of Days of a Month in R (2 Examples)


In this article, I’ll explain how to get the number of days in a certain month in R.

Table of contents:

Let’s jump right to the R code…


Creation of Example Data

The following data is used as a basement for this R programming tutorial:

my_date <- as.Date("2023-04-10")    # Create example date
my_date                             # Print example date
# [1] "2023-04-10"

As you can see based on the previous output of the RStudio console, our example data is a date object representing the 10th of April in the year 2023.


Example 1: Get Number of Days in a Certain Month Using days_in_month() Function of lubridate Package

The following R syntax demonstrates how to calculate the number of days in a specific month using the lubridate package.

To be able to use the functions of the lubridate add-on package, we first need to install and load lubridate:

install.packages("lubridate")       # Install lubridate package
library("lubridate")                # Load lubridate

Next, we can count the number of days in the month of our date object using the days_in_month function:

days_in_month(my_date)              # Print days in month
# Apr 
#  30

As you can see, the month of our date (i.e. April) has 30 days.


Example 2: Get Number of Days in a Certain Month Using monthDays() Function of Hmisc Package

Alternatively to the lubridate package, we can also use the Hmisc package to get the number of days of a particular month.

In order to use the functions of the Hmisc package, we first have to install and load Hmisc:

install.packages("Hmisc")           # Install & load Hmisc package

Next, we can apply the monthDays function to our date object:

monthDays(my_date)                  # Print days in month
#  30

Once again, the value 30 was returned.


Video & Further Resources

In case you need further explanations on the R programming codes of the present tutorial, I recommend watching the following video on my YouTube channel. I’m explaining the R syntax of this page in the video tutorial:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you might want to have a look at the related R articles on my homepage. Some other articles about topics such as numeric values, indices, and data inspection are listed below.


In summary: In this article you have learned how to find out the number of days in a date object in R programming. In case you have any additional questions, let me know in the comments below.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.