Get End of Month for Certain Date in R (2 Examples)

 

This page illustrates how to find the last day of the month for a particular date in R.

Table of contents:

You’re here for the answer, so let’s get straight to the examples:

 

Creation of Example Data

Let’s first construct some example data:

my_dates <- as.Date(c("2022-05-10",                         # Create example dates
                      "2024-02-17",
                      "2023-06-01"))
my_dates                                                    # Print example dates
# [1] "2022-05-10" "2024-02-17" "2023-06-01"

The previous RStudio console output shows the structure of the exemplifying data – We have created a vector of dates.

Note that these dates are already properly formatted as the Date class by applying the as.Date function to our vector in the creation process.

 

Example 1: Get Last Dates of Month Using ceiling_date() Function of lubridate Package

This example illustrates how to identify the last day of the months that correspond to our dates using the lubridate package.

If we want 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 package

In the next step, we can apply the ceiling_date and days functions to our date vector to get the last day of the corresponding months:

my_dates_end1 <- ceiling_date(my_dates, "month") - days(1)  # End of month
my_dates_end1                                               # Print new dates
# [1] "2022-05-31" "2024-02-29" "2023-06-30"

Have a look at the previous RStudio console output: It shows the three last days of the months that correspond to our input dates.

 

Example 2: Get Last Dates of Month Using timeLastDayInMonth() Function of timeDate Package

Example 2 demonstrates how to use the timeDate package to find the end of the month for a certain date.

If we want to use the functions of the timeDate package, we first need to install and load timeDate:

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

Next, we can apply the timeLastDayInMonth function to return the last days of each month:

my_dates_end2 <- timeLastDayInMonth(my_dates)               # End of month
my_dates_end2                                               # Print new dates
# GMT
# [1] [2022-05-31] [2024-02-29] [2023-06-30]

The output of the timeLastDayInMonth looks slightly different compared to the lubridate package code. However, the returned dates are the same.

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. I’m showing the R programming code of this article in the video:

 

 

Furthermore, you might read some of the related articles on my website:

 

In this article, I have demonstrated how to get the end of the month for a specific date, or how to convert a vector of dates to the corresponding end of the month for each of these dates in the R programming language. In case you have further questions, please let me know in the comments.

 

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.

Top