Dates that will not behave
Our dates look fine on screen, but they sort in a strange order, the chart's time axis is a mess, and math with them fails. Most of the time they are not dates at all. They are text that looks like dates.
What we need
- R 4.1 or newer
- lubridate
- A few dates (we make them below)
Text that looks like dates
These came from a spreadsheet as month/day/year. R sees text, so it sorts them letter by letter: December comes before July.
d <- c("12/25/2026", "07/04/2026", "03/09/2026")
class(d)
sort(d)Loading the checked output…
Let R guess, and watch it go wrong
as.Date() only knows date formats that start with the year, like 2026-12-25. Our dates start with the month, so R stops with an error instead of guessing.
as.Date(d)Loading the checked output…
Fix 1: tell R the format
%m is the month, %d the day and %Y the four-digit year. Write them in the same order as our text, with the same slashes.
dates <- as.Date(d, format = "%m/%d/%Y")
dates
sort(dates)Loading the checked output…
Fix 2: let lubridate read it
lubridate has a reader named after each order: mdy() for month-day-year, dmy() for day-month-year, ymd() for year-month-day.
library(lubridate)
mdy(d)Loading the checked output…
Mixed formats in one column
Real files often mix formats. parse_date_time() tries each order we list, row by row.
mixed <- c("2026-03-09", "07/04/2026", "December 25, 2026")
parse_date_time(mixed, orders = c("ymd", "mdy", "B d, Y"))Loading the checked output…
Dates that arrive as numbers
Excel stores dates as a count of days. If we see 46000-something where a date should be, it came from Excel. Excel counts from December 30, 1899.
as.Date(46030, origin = "1899-12-30")Loading the checked output…
Taste test
After turning text into dates, check that none failed (they become NA) and that the first and last dates make sense.
stopifnot(!anyNA(dates))
range(dates)Loading the checked output…
Kitchen tip: the habit that prevents this
Turn text into real dates as soon as we read a file, with the exact format. Then check for NA and look at the first and last date.
Three common mix-ups
- Mixing up %m (month) and %M (minutes).
- Using %y (two-digit year) for a four-digit year, or the other way around.
- Day-month-year files from outside the United States read as month-day-year: 03/04 is March 4 in New York and April 3 in London.
Quick check
Our file has dates like "25.12.2026". Which reader do we use?
Practice: try it here
These dates came in day/month/year order. Turn them into real dates called my_dates, so the first one is January 31, 2026.
Change the code in the box, then press Run to see what it does, and Check my answer when it looks right. It all runs in this browser, so nothing is sent anywhere.
Tell as.Date() the order: "%d/%m/%Y" means day, month, then the four-digit year. lubridate::dmy() works too.
x <- c("31/01/2026", "15/02/2026", "01/03/2026")
my_dates <- as.Date(x, format = "%d/%m/%Y")
my_dates
class(my_dates)- class(x): text or Date?
- as.Date(x, format = "%m/%d/%Y")
- lubridate: mdy(), dmy(), ymd()
- check: anyNA() and range()
Keep the recipe card
A one-picture summary to pin next to a screen or send to a friend who is stuck on the same thing.
Trying this on other data? Check the row counts and results before and after each step. Package versions change, so if something here works differently, please tell us. Every output on this page comes from running the code above in R; please cross-check before relying on it for our work.