How to format a date in R
· 1 min read · Updated March 15, 2026 · beginner
r dates lubridate formatting
Formatting dates is essential for reporting and data visualization. Here are several approaches.
With base R
Use format() with strptime codes:
# Create a date
date_obj <- as.Date("2026-03-15")
# Format as Year-Month-Day
format(date_obj, "%Y-%m-%d")
# [1] "2026-03-15"
# Format as Month Day, Year
format(date_obj, "%B %d, %Y")
# [1] "March 15, 2026"
# Short format
format(date_obj, "%m/%d/%y")
# [1] "03/15/26"
Common format codes:
- %Y — 4-digit year
- %y — 2-digit year
- %m — month as number
- %B — full month name
- %b — abbreviated month
- %d — day of month
With lubridate
The lubridate package makes this intuitive:
library(lubridate)
date_obj <- ymd("2026-03-15")
# Easy formatting
stamp("March 15, 2026")(date_obj)
# [1] "March 15, 2026"
# Extract parts
year(date_obj)
# [1] 2026
month(date_obj, label = TRUE)
# [1] Mar
day(date_obj)
# [1] 15
Common formats
x <- Sys.Date()
# ISO 8601
format(x, "%Y-%m-%d")
# US style
format(x, "%m/%d/%Y")
# European style
format(x, "%d/%m/%Y")
# With time
format(Sys.time(), "%Y-%m-%d %H:%M:%S")
See Also
- r-date-time-lubridate — Full guide to lubridate
- how-to-convert-string-to-date — Convert strings to dates