Convert character strings to dates in R
How to convert character strings to dates in R with base as.Date() and lubridate, what the conversion changes, and how to keep dates unambiguous when you share them.
Dates often reach R as text. They might come from a CSV column, a form export or a pasted list. As character strings, they sort alphabetically, cannot be subtracted, and are read differently depending on whether the writer used US or day-first notation. Converting them to R's Date class fixes those problems. However, the conversion is only as reliable as the format you tell R to expect. This review covers what the conversion changes, when it is the right step, and how to keep the result readable once it leaves R.
Is converting to a Date the right choice?
Convert when you need to sort chronologically, filter by range, compute intervals, group by month or year, or plot along a time axis. The main benefit is that R then treats the values as points in time rather than as strings of characters.
The decisive tradeoff is interpretation. Every conversion commits to one reading of the input. Take "01/02/2024". Under month/day/year it means January 2, and under day/month/year it means February 1. R cannot tell which one the source intended. If you choose the wrong format, the result can be wrong without any warning, or it can be filled with missing values.
Keep the original text when the values are labels rather than dates. Examples include fiscal period names, partial dates such as "Spring 2024", or entries whose format you cannot confirm. You can also keep the raw column alongside the converted one so you can audit any value that failed or looked suspicious.
What the conversion actually changes
Conversion changes the stored data, not just how it looks. A character vector holds text. A Date vector stores a number: the count of days since 1970-01-01, plus a class attribute that tells R to print it as a date. The familiar "2024-03-15" you see in the console is a presentation of that number.
x <- "2024-03-15"
d <- as.Date(x)
class(x) # "character"
class(d) # "Date"
as.numeric(d) # 19797 (days since 1970-01-01)
d + 30 # "2024-04-14"
The reverse step is format(). It turns a Date back into text in whatever notation you specify. The underlying value stays the same, and only the output string differs.
format(d, "%m/%d/%Y") # "03/15/2024"
format(d, "%d %B %Y") # "15 March 2024" (in an English locale)
Base R: as.Date() with a format string
If you give as.Date() no format, it tries year-month-day patterns with hyphens or slashes. With any other layout, you must describe the input using strptime-style codes. When you supply an explicit format, strings that do not match it become NA. Without a format, as.Date() stops with an error if it cannot recognize the first non-missing value.
as.Date("03/15/2024", format = "%m/%d/%Y") # "2024-03-15"
as.Date("15.03.2024", format = "%d.%m.%Y") # "2024-03-15"
as.Date("March 15, 2024", format = "%B %d, %Y") # "2024-03-15" (English locale)
as.Date("2024-15-03", format = "%Y-%m-%d") # NA
| Code | Meaning | Example |
| %Y | Four-digit year | 2024 |
| %y | Two-digit year | 24 |
| %m | Month as number | 03 |
| %d | Day of month | 15 |
| %B | Full month name (locale-dependent) | March |
| %b | Abbreviated month name (locale-dependent) | Mar |
lubridate: functions named by component order
The lubridate package swaps format strings for functions named after the order of the date parts: ymd(), mdy(), dmy() and their variants. Each one accepts a range of separators and month spellings, which helps with messy input. When values cannot be parsed, lubridate returns NA for them and issues a warning that reports how many failed. That warning is worth reading rather than suppressing.
library(lubridate)
mdy("03/15/2024") # "2024-03-15"
mdy("March 15, 2024") # "2024-03-15"
dmy("15.03.2024") # "2024-03-15"
ymd("20240315") # "2024-03-15"
If one column mixes several layouts, parse_date_time() accepts a vector of candidate orders, such as c("mdy", "ymd"). This is convenient, but it does not resolve genuine ambiguity. An entry such as "01/02/2024" can still match more than one order. Use it only when the source documents which conventions it used.
Choose base R when the format is fixed and known and you want no extra dependency. Choose lubridate when inputs vary in separators or month spellings, or when you prefer readable function names over format codes. Both return the same Date class, so downstream code does not care which one you used.
Pitfalls that change meaning
- Day-month order: confirm the source convention before choosing %m/%d or %d/%m, or mdy() or dmy(). US sources usually put the month first, but that is not guaranteed.
- Two-digit years: R's documentation maps %y values 00–68 to the 2000s and 69–99 to the 1900s. A birth year written as "65" therefore becomes 2065.
- Month names: %B, %b and lubridate's name matching depend on the session locale. A script that parses English month names may fail on another machine with a different language setting.
- Silent NAs: after converting, count missing values and compare them with the missing values in the original column. New NAs mean some strings did not match.
- Date-times: if the strings include a time of day, the Date class drops it. Use a date-time parser such as as.POSIXct() or ymd_hms() and set the time zone explicitly, because lubridate's date-time parsers default to UTC.
raw <- c("03/15/2024", "3/16/2024", "2024-03-17")
d <- as.Date(raw, format = "%m/%d/%Y")
raw[is.na(d) & !is.na(raw)] # "2024-03-17" did not match the format
Will the result stay usable after export or sharing?
Inside R, a Date is unambiguous. Once it is written out, it is text again, and the receiving tool has to interpret it. Base R's write.csv() writes Date columns in year-month-day form, such as 2024-03-15. That ISO 8601 layout is the least ambiguous choice for files exchanged between systems, because it has only one common reading.
Spreadsheet applications and other tools may re-parse date-like text on import according to their own regional settings. This review includes no tests of specific applications. Check how your particular destination handles the column rather than assuming that one successful import generalizes.
For human readers, the ideal display format differs from the ideal storage format. In a report or a table meant to be read, a written-out form such as "March 15, 2024" avoids the day/month confusion of all-numeric notation. Produce it with format() at the presentation stage, and keep the Date column for any further calculation. No documentation or tests of how specific assistive technologies announce different date formats were available for this review, so that question remains open.
Bottom line
Converting character strings to dates in R is worthwhile whenever you need date arithmetic, sorting or plotting. The conversion preserves meaning only if the format you specify matches how the source wrote the dates. Confirm that convention, check for new missing values after parsing, export in year-month-day form, and format dates for human readers only at the final display step. Leave values as plain text when they are labels, partial dates or of uncertain origin.