How to Convert Text to a Date with as.Date in R
A practical guide to as.Date in R: turn date strings into Date values, choose format codes, avoid silent NA results and verify the conversion.
Dates often arrive in R as plain text: a CSV column like "2024-03-15", a log line, or a form field typed as "03/15/2024". R treats these values as character strings, so you cannot sort them chronologically, subtract them, or group them by month until you convert them. The base function as.Date does that conversion. This guide explains what it does to your text, how to tell it which pattern to read, and how to check that nothing was lost or misread.
What as.Date does to your text
as.Date takes a character vector and returns a vector of class Date. Internally, a Date is stored as the number of days since 1970-01-01, which is why date arithmetic and sorting work after conversion. It is printed in ISO form (year-month-day) regardless of how the original text looked.
- Input: character strings containing a calendar date. It also accepts other types, such as numbers of days or date-time objects, but this guide focuses on text.
- Output: a Date vector of the same length as the input. Each element is either a parsed date or NA if that string could not be read with the chosen format.
- Scope: calendar dates only. Any time of day in the text is not kept. If you need hours and minutes, use a date-time function such as as.POSIXct or strptime instead.
- Effect on the source: as.Date returns a new object and does not modify the original vector. Assign the result to a new variable or column if you want to keep the original text for comparison.
The basic conversion
If your text is already in year-month-day form with hyphens or slashes, as.Date can read it without extra arguments. By default, it tries the formats "%Y-%m-%d" and "%Y/%m/%d".
x <- c("2024-03-15", "2024-12-01")
d <- as.Date(x)
class(d) # "Date"
d[2] - d[1] # difference in days
Any other layout needs an explicit format argument. A format string describes the text pattern. Percent codes stand for date parts, and every other character must match the text literally.
as.Date("03/15/2024", format = "%m/%d/%Y")
as.Date("15.03.2024", format = "%d.%m.%Y")
as.Date("15 March 2024", format = "%d %B %Y")
as.Date("20240315", format = "%Y%m%d")
Common format codes
| Code | Meaning | Example text |
| %Y | Four-digit year | 2024 |
| %y | Two-digit year | 24 |
| %m | Month as a number | 03 |
| %d | Day of the month | 15 |
| %B | Full month name (depends on locale) | March |
| %b | Abbreviated month name (depends on locale) | Mar |
| %j | Day of the year | 075 |
Separators are part of the pattern. "15.03.2024" needs dots in the format, and "15 March 2024" needs spaces. If the separator in your text differs from the one in the format, that element becomes NA.
Pitfalls that change meaning without warning
Ambiguous day and month order
A string such as "03/04/2024" can mean March 4 or April 3. R cannot know which one you mean, so always state the format for slash- or dot-separated dates that are not year-first. Without a format, as.Date tries the year-first defaults. For some inputs, this produces an error saying the string is not in a standard unambiguous format. For others, it may silently produce a meaningless result, for example a date in year 3, because the first digits were read as the year. Treat any very old or far-future year in your output as a sign that the format was wrong.
One format for the whole vector
When you rely on the default formats, as.Date chooses a format based on the first non-missing element and applies it to every element. Strings in a different layout then become NA without an error. If a column mixes layouts, convert each layout separately and combine the results. One approach is to try one format and then fill the remaining NA values with a second format.
x <- c("2024-03-15", "15/03/2024")
d <- as.Date(x, format = "%Y-%m-%d")
missing <- is.na(d)
d[missing] <- as.Date(x[missing], format = "%d/%m/%Y")
Trailing text is ignored
Parsing reads only as much of each string as the format requires, and the rest is discarded. For example, as.Date("2024-03-15 10:30:00") keeps only the date. This is convenient for timestamps, but it also means extra text after the date will not produce an error, so it cannot alert you to unexpected content.
Two-digit years
With %y, R maps 00 to 68 to the years 2000 to 2068, and 69 to 99 to the years 1969 to 1999. A birth year written as "55" therefore becomes 2055. If your data spans that boundary, correct the century after conversion or obtain four-digit years from the source.
Month names and locale
%B and %b match month names in the current time locale (the LC_TIME setting). English names such as "March" may fail to parse on a system set to another language. You can temporarily switch to the C locale, which uses English names, and then restore the previous setting.
old <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
d <- as.Date("15 March 2024", format = "%d %B %Y")
Sys.setlocale("LC_TIME", old)
Extracting a date from longer text first
If the date is embedded in a sentence or a file name, pull it out with a regular expression before converting it. The example below finds an ISO-style date in each string. It keeps NA where no date appears, so the result stays aligned with the original vector.
x <- c("Invoice issued 2024-03-15 by email",
"report_2024-11-02_final.csv",
"no date here")
m <- regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", x)
found <- rep(NA_character_, length(x))
found[m != -1] <- regmatches(x, m)
d <- as.Date(found, format = "%Y-%m-%d")
Calling regmatches(x, m) on its own returns only the matches, so its length can differ from the input. Filling a full-length vector, as shown above, preserves the position of each row.
Numbers that represent dates
Sometimes a date column arrives as a day count instead of text, for example when spreadsheet dates are imported as numbers. as.Date can convert these values if you supply the origin, which is the date that day 0 refers to. Since R 4.3.0, the origin defaults to 1970-01-01. Older versions require it explicitly. For day counts exported from spreadsheet software that use the common 1900 date system, an origin of "1899-12-30" is typically used. Confirm this against a few known dates in your own file.
as.Date(45366, origin = "1899-12-30")
Verifying the result
Because failed conversions become NA rather than errors, check the output explicitly before relying on it.
- Confirm the type: class(d) should return "Date".
- Count failures: sum(is.na(d) & !is.na(x)) shows how many non-missing strings could not be parsed.
- Inspect the failures: x[is.na(d) & !is.na(x)] lists the original text that did not match, which usually reveals a different separator, layout or language.
- Check the range: range(d, na.rm = TRUE) quickly exposes impossible years caused by a wrong format or two-digit years.
- Spot-check against the source: compare a few rows side by side, for example head(data.frame(original = x, parsed = d)).
Displaying the date in a different layout
Converting a date sets its value, not its appearance. To show a Date in another layout, use format, which returns text again. Keep the Date version for calculations, and use the formatted text only for display or export.
d <- as.Date("2024-03-15")
format(d, "%d/%m/%Y")
format(d, "%B %d, %Y")
A note on sources
No subject-specific evidence, such as benchmarks or version-specific test results, was supplied for this article. The behavior described here reflects the documented behavior of R's base date functions, and the code samples are illustrative examples rather than verified output. Exact results for month names depend on your locale, so run the verification checks above on your own data.