How to convert a string to a date in R
· 1 min read · Updated March 14, 2026 · beginner
r dates lubridate string-conversion
Converting strings to dates is essential when working with real-world data. Here are the main approaches.
With base R
Use as.Date() with a format string:
# Default ISO format: YYYY-MM-DD
as.Date("2026-03-14")
# [1] "2026-03-14"
# Other formats
as.Date("03/14/2026", format = "%m/%d/%Y")
# [1] "2026-03-14"
as.Date("March 14, 2026", format = "%B %d, %Y")
# [1] "2026-03-14"
Common format codes: %Y (4-digit year), %y (2-digit year), %m (month), %d (day), %B (full month name), %b (abbreviated month).
With lubridate
The lubridate package makes this easier with functions that auto-detect common formats:
library(lubridate)
# Auto-detection
ymd("2026-03-14")
# [1] "2026-03-14"
mdy("March 14, 2026")
# [1] "2026-03-14"
dmy("14/03/2026")
# [1] "2026-03-14"
ymd_hms("2026-03-14 10:30:00")
# [1] "2026-03-14 10:30:00 UTC"
With readr
The readr package parses dates efficiently during import:
library(readr)
parse_date("2026-03-14")
# [1] "2026-03-14"
# Specify format if auto-detection fails
parse_date("14-Mar-2026", format = "%d-%b-%Y")
# [1] "2026-03-14"
Handling messy dates
For inconsistent formats in a column, lubridate’s parse_date_time() handles multiple formats:
library(lubridate)
dates <- c("2026-03-14", "03/14/2026", "March 14, 2026")
parse_date_time(dates, orders = c("ymd", "mdy", "Bdy"))
# [1] "2026-03-14" "2026-03-14" "2026-03-14"
See Also
- r-date-time-lubridate — Full guide to lubridate
- lubridate-dates — Tutorial on date handling