R: How to Convert Character to Date Using Lubridate

Ribridate is a package in R that allows you to convert character data to date formats such as month-day-year, day-month-year and year-month-day. This can be useful for data analysis, as it allows for easier manipulation of dates and times. The lubridate package provides a range of functions to make it easy to convert characters to dates, as well as other tasks such as finding the difference between two dates or adding and subtracting time intervals.


You can use various functions from the lubridate package in R to convert a character column to a date format.

Two of the most common functions include:

  • ymd() – Convert character in year-month-date format to date
  • mdy() – Convert character in month-day-year format to date

The following examples show how to use the ymd() and mdy() functions in practice.

Note: Refer to the for a complete list of functions you can use to convert characters to dates depending on the format your dates are in.

Example 1: Convert Character to Date Using ymd()

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(date=c('2022-01-05', '2022-02-18', '2022-03-21',
                        '2022-09-15', '2022-10-30', '2022-12-25'),
                 sales=c(14, 29, 25, 23, 39, 46))

#view data frame
df

        date sales
1 2022-01-05    14
2 2022-02-18    29
3 2022-03-21    25
4 2022-09-15    23
5 2022-10-30    39
6 2022-12-25    46

#view class of date column
class(df$date)

[1] "character"

Currently the values in the date column are characters, but we can use the ymd() function from the lubridate package to convert them to dates:

library(lubridate)

#convert character to date format
df$date <- ymd(df$date)

#view updated data frame
df

        date sales
1 2022-01-05    14
2 2022-02-18    29
3 2022-03-21    25
4 2022-09-15    23
5 2022-10-30    39
6 2022-12-25    46

#view updated class of date column
class(df$date)

[1] "Date"

We can see that the date column now has a class of Date instead of character.

Example 2: Convert Character to Date Using mdy()

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(date=c('March 4, 2022', 'April 9, 2022', 'May 6, 2022',
                        'May 29, 2022', 'June 1, 2022', 'July 2, 2022'),
                 sales=c(14, 29, 25, 23, 39, 46))

#view data frame
df

           date sales
1 March 4, 2022    14
2 April 9, 2022    29
3   May 6, 2022    25
4  May 29, 2022    23
5  June 1, 2022    39
6  July 2, 2022    46

#view class of date column
class(df$date)

[1] "character"

Currently the values in the date column are characters, but we can use the mdy() function from the lubridate package to convert them to dates:

library(lubridate)

#convert character to date format
df$date <- mdy(df$date)

#view updated data frame
df

        date sales
1 2022-03-04    14
2 2022-04-09    29
3 2022-05-06    25
4 2022-05-29    23
5 2022-06-01    39
6 2022-07-02    46

#view updated class of date column
class(df$date)

[1] "Date"

We can see that the date column now has a class of Date instead of character.

The following tutorials explain how to perform other common tasks in R:

x