How to Format Dates in R with format() and lubridate
To format dates in R for reports, plots, or exports, use the base R format() function with strptime format codes. It converts Date objects to character strings in any pattern you specify — ISO 8601, US month-day-year, European day-month-year, or custom arrangements. Here are the most common patterns you’ll use in practice.
date_obj <- as.Date("2026-03-15")
# Common format patterns
format(date_obj, "%Y-%m-%d") # "2026-03-15" (ISO 8601)
format(date_obj, "%B %d, %Y") # "March 15, 2026"
format(date_obj, "%m/%d/%y") # "03/15/26"
format(date_obj, "%d/%m/%Y") # "15/03/2026" (European)
# Key codes: %Y=4-digit year, %m=month number, %B=full month,
# %b=abbreviated month, %d=day, %A=weekday name
Use %B and %A with caution — they produce locale-dependent output. On a system set to French, %B prints “mars” instead of “March.” For consistent results across systems, stick to numeric codes or set the locale with Sys.setlocale("LC_TIME", "C").
library(lubridate)
date_obj <- ymd("2026-03-15")
# Extract date parts
year(date_obj) # 2026
month(date_obj, label = TRUE) # Mar
day(date_obj) # 15
# Create a reusable formatter
fmt <- stamp("March 15, 2026")
fmt(date_obj) # "March 15, 2026"
lubridate::stamp() creates a formatting function from an example date string — feed it one formatted date and it returns a function that applies the same format to any Date object. This is useful when you want consistent date display across a report without repeating format() calls. For date-time values that include hours and seconds, use format(Sys.time(), "%Y-%m-%d %H:%M:%S") to capture the full timestamp. The lubridate::ymd_hms() parser reads date-times in ISO-like order and returns a POSIXct object ready for formatting.
See also
- r-date-time-lubridate, Full guide to lubridate
- how-to-convert-string-to-date, Convert strings to dates