lubridate::ymd
ymd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale('LC_TIME'), truncated = 0) Date | POSIXct · Updated March 20, 2026 · Tidyverse ymd() parses dates in year-month-day order from character strings or numbers. It is the most commonly used parser in lubridate because machine-generated date formats — CSV exports, database dumps, API responses — tend to follow this arrangement. The function auto-detects separators (hyphens, slashes, spaces, or none) and handles mixed formats within a single vector without you specifying a format string.
Syntax
ymd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"), truncated = 0)
ymd() is part of a family of parsers sharing the same signature. The function name encodes the element order:
| Function | Element order | Example input | Parsed as |
|---|---|---|---|
ymd() | year, month, day | "2009-01-02" | 2009-01-02 |
ydm() | year, day, month | "2009-02-01" | 2009-01-02 |
mdy() | month, day, year | "01-02-2009" | 2009-01-02 |
myd() | month, year, day | "01-2009-02" | 2009-01-02 |
dmy() | day, month, year | "02-01-2009" | 2009-01-02 |
dym() | day, year, month | "02-2009-01" | 2009-01-02 |
yq() | year, quarter | "2014.2" | 2014-04-01 |
ym() | year, month | "2012-06" | 2012-06-01 |
my() | month, year | "Jan 2009" | 2009-01-01 |
Use ymd_hms() (and variants) when you need to parse times as well.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | character or numeric | Required | A vector of suspected dates. Each element may use a different format. |
quiet | logical | FALSE | Suppress parsing progress messages. Set to TRUE for large vectors. |
tz | character | NULL | Timezone. NULL returns a Date; any other value returns POSIXct. |
locale | character | Sys.getlocale("LC_TIME") | Locale for parsing month names. Rarely needs changing. |
truncated | integer | 0 | Allow N rightmost components to be missing. truncated = 2 parses "2012" as "2012-01-01". |
Return Value
Returns a Date object by default. When tz is set, returns a POSIXct object with the specified timezone instead.
ymd("20090101")
# [1] "2009-01-01"
# class: Date
ymd("20090101", tz = "UTC")
# [1] "2009-01-01 UTC"
# class: POSIXct POSIXt
Examples
Basic usage
library(lubridate)
# From character — any separator works
ymd("2009-01-01")
# [1] "2009-01-01"
# From numeric — compact form, no separator needed
ymd(20090101)
# [1] "2009-01-01"
Multiple dates at once
# Multiple arguments — each argument becomes one element
ymd(090101, 90102)
# [1] "2009-01-01" "2009-01-02"
# Single vector with mixed formats
ymd(c(20090101, "2009-01-02", "2009 01 03", "2009-1-4"))
# [1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04"
Truncated dates
When some components are missing, set truncated to allow the parser to fill in defaults (day and month default to 01):
# Year and month only — day becomes 01
ymd("2012-06", truncated = 1)
# [1] "2012-06-01"
# Year only — both month and day become 01
ymd("2012", truncated = 2)
# [1] "2012-01-01"
With timezone
# POSIXct when tz is specified
ymd("2009-01-01", tz = "America/New_York")
# [1] "2009-01-01 EST"
Using with dplyr
library(dplyr)
events <- data.frame(
date_chr = c("20240315", "20240316", "20240317"),
event = c("login", "purchase", "logout")
)
events %>%
mutate(date = ymd(date_chr))
# date_chr event date
# 1 20240315 login 2024-03-15
# 2 20240316 purchase 2024-03-16
# 3 20240317 logout 2024-03-17
Common problems
“All formats failed to parse”: When the input has too many NA values or non-date strings, lubridate cannot guess the format reliably. Switch to parse_date_time() with an explicit format list.
Two-digit years: ymd("990101") is parsed as 2099-01-01 in current lubridate. The century interpretation has changed across lubridate versions — do not rely on it for two-digit years without explicit conversion.
NA with warning: Individual unparseable elements return NA with a warning listing how many failed. This is not an error — check is.na() on the result to find the problem elements.
See Also
stringr::str_detect— Detect patterns in character vectors (complements date extraction workflows)stringr::str_extract— Extract matching substrings from character vectors