01 / 20

lubridate
Functions in R

Learn to parse, manipulate, and reason about dates and times using the lubridate package: ymd(), mdy(), dmy(), and more.

R Programming

Why Are Dates So Tricky?

Dates come in many formats: "2024-03-15", "March 15, 2024", "15/03/2024". R's base functions often misinterpret these. The lubridate package solves this by letting you tell R the order of components: year, month, and day.

What is lubridate?

R Package
Part of the tidyverse ecosystem. Install with install.packages("lubridate").
Purpose
Makes it easy to parse, extract, and do arithmetic with dates and times.
Key Benefit
You describe the order of date parts: no cryptic format strings like "%m/%d/%Y".
library(lubridate) # That's it! You're ready to parse dates.

Strings vs. Date Objects

A character string like "2024-03-15" is just text: R cannot do math on it. A Date object stores the date internally as a number, enabling subtraction, comparison, and extraction of components.

Character String "March 15, 2024" Just text: no math mdy() Date Object 2024-03-15 Math, sort, compare

The Core Parsing Functions

The function name tells R the order of the components in your string. Each letter stands for: y = year, m = month, d = day.

ymd()
Year, Month, Day
"2024-03-15"
"2024/03/15"
"20240315"
mdy()
Month, Day, Year
"03-15-2024"
"March 15, 2024"
"3/15/24"
dmy()
Day, Month, Year
"15-03-2024"
"15 March 2024"
"15/03/24"

The Golden Rule

ymd() "2024-03-15" mdy() "March 15, 2024" dmy() "15/03/2024" Date: 2024-03-15

Flexible Separators

lubridate doesn't care about separators. Dashes, slashes, spaces, commas, or even no separator at all: as long as the order matches the function name, it works.

ymd("2024-03-15") # dashes ymd("2024/03/15") # slashes ymd("2024 03 15") # spaces ymd("20240315") # no separator # All produce: 2024-03-15

ymd vs mdy vs dmy

Feature ymd() mdy() dmy()
Order Year, Month, Day Month, Day, Year Day, Month, Year
Common in ISO standard, databases USA Europe, most of world
Example input "2024-03-15" "03/15/2024" "15-03-2024"
Output 2024-03-15 (same Date object)

Adding Time: ymd_hms()

Append _hms, _hm, or _h to any parsing function to include time components: h = hours, m = minutes, s = seconds.

ymd_hms("2024-03-15 14:30:00") # "2024-03-15 14:30:00 UTC" mdy_hm("March 15, 2024 2:30pm") # "2024-03-15 14:30:00 UTC" dmy_h("15-03-2024 14") # "2024-03-15 14:00:00 UTC"

How to Apply: Step by Step

1
Look at your data. What order are the date components in? Year first? Month first? Day first?
2
Pick the matching function. The function name spells out the order: ymd, mdy, or dmy.
3
Check for time. If there are hours/minutes/seconds, append _hms, _hm, or _h.
4
Pass the string. Don't worry about separators: lubridate figures them out automatically.
5
Verify the output. The result should print as a clean, standardized date (YYYY-MM-DD).

Example 1: Simple Parsing

You have the string "2024-07-04". Which function should you use?

# The string: "2024-07-04" # Order: Year (2024), Month (07), Day (04) # Match: ymd() my_date <- ymd("2024-07-04") my_date # [1] "2024-07-04" class(my_date) # [1] "Date"

Example 2: US-Format Dates

A dataset has dates like "July 4, 2024" and "12/25/2023". How do we parse them?

# "July 4, 2024" → Month, Day, Year mdy("July 4, 2024") # [1] "2024-07-04" # "12/25/2023" → Month, Day, Year mdy("12/25/2023") # [1] "2023-12-25" # Works with a vector too! dates <- c("July 4, 2024", "12/25/2023") mdy(dates) # [1] "2024-07-04" "2023-12-25"

Example 3: European Format with Time

A log file has timestamps like "15/03/2024 14:30:00". The day comes first (European format), and there's a time component.

# "15/03/2024 14:30:00" # Order: Day, Month, Year + Hours, Minutes, Seconds # Match: dmy_hms() timestamp <- dmy_hms("15/03/2024 14:30:00") timestamp # [1] "2024-03-15 14:30:00 UTC" class(timestamp) # [1] "POSIXct" "POSIXt"

Extracting Components

Once you have a Date object, lubridate provides accessor functions to pull out individual parts.

my_date <- ymd("2024-03-15") year(my_date) # 2024 month(my_date) # 3 day(my_date) # 15 wday(my_date) # 6 (Friday) wday(my_date, label = TRUE) # Fri
year()
Extracts the 4-digit year
month()
Extracts the month (1-12)
day()
Extracts the day of month
wday()
Day of the week (1-7)

Date Arithmetic

lubridate lets you add and subtract time using helper functions like days(), months(), and years().

start <- ymd("2024-01-01") start + days(30) # "2024-01-31" start + months(3) # "2024-04-01" start + years(1) # "2025-01-01" # Subtract two dates ymd("2024-12-31") - ymd("2024-01-01") # Time difference of 365 days

Common Mistakes

✕ Wrong Function Order
Using ymd() on "03/15/2024" gives NA because "03" is parsed as the year. Use mdy() instead.
✕ Forgetting _hms
Using ymd() on "2024-03-15 14:30:00" drops the time. Append _hms to capture hours, minutes, seconds.
✕ Not Loading lubridate
Calling ymd() without library(lubridate) first gives "Error: could not find function". Load the package first.

Question 1 of 5

Which function would you use to parse the string "July 4, 2024"?

Question 2 of 5

What does ymd("20231225") return?

Question 3 of 5

A European dataset contains dates like "25/12/2023". Which function correctly parses this as December 25, 2023?

Question 4 of 5

You want to parse "2024-03-15 09:45:30" including the time. Which function should you use?

Question 5 of 5

What does month(ymd("2024-08-20")) return?