Format vector values to date values using one of fourteen presets. Input can
be in the form of POSIXt
(i.e., date-times), the Date
type, or
character
(must be in the ISO 8601 form of YYYY-MM-DD HH:MM:SS
or
YYYY-MM-DD
).
We can simply apply a preset date style to format the dates. The following
date styles are available for use (all using the input date of 2000-02-29
in the example output dates):
"iso"
: 2000-02-29
"wday_month_day_year"
: Tuesday, February 29, 2000
"wd_m_day_year"
: Tue, Feb 29, 2000
"wday_day_month_year"
: Tuesday 29 February 2000
"month_day_year"
: February 29, 2000
"m_day_year"
: Feb 29, 2000
"day_m_year"
: 29 Feb 2000
"day_month_year"
: 29 February 2000
"day_month"
: 29 February
"year"
: 2000
"month"
: February
"day"
: 29
"year.mn.day"
: 2000/02/29
"y.mn.day"
: 00/02/29
We can use the info_date_style()
function for a useful reference on all of
the possible inputs to date_style
.
vec_fmt_date(
x,
date_style = 2,
pattern = "{x}",
output = c("auto", "plain", "html", "latex", "rtf", "word")
)
A character vector.
A numeric vector.
The date style to use. Supply a number (from 1
to 14
)
that corresponds to the preferred date style, or, provide a named date
style ("wday_month_day_year"
, "m_day_year"
, "year.mn.day"
, etc.). Use
info_date_style()
to see the different numbered and named date presets.
A formatting pattern that allows for decoration of the
formatted value. The value itself is represented by {x}
and all other
characters are taken to be string literals.
The output style of the resulting character vector. This can
either be "auto"
(the default), "plain"
, "html"
, "latex"
, "rtf"
,
or "word"
. In knitr rendering (i.e., Quarto or R Markdown), the
"auto"
option will choose the correct output
value
Let's create a character vector of dates in the ISO-8601 format for the next few examples:
str_vals <- c("2022-06-13", "2019-01-25", "2015-03-23", NA)
Using vec_fmt_date()
with the default options will create a character
vector of formatted dates. Any NA
values remain as NA
values. The
rendering context will be autodetected unless specified in the output
argument (here, it is of the "plain"
output type).
vec_fmt_date(str_vals)
#> [1] "Monday, June 13, 2022" "Friday, January 25, 2019"
#> [3] "Monday, March 23, 2015" NA
We can change the formatting style by choosing a number from 1
to 14
:
vec_fmt_date(str_vals, date_style = 6)
#> [1] "Jun 13, 2022" "Jan 25, 2019" "Mar 23, 2015" NA
We can always use info_date_style()
to call up an info table that serves as
a handy reference to all of the date styles.
As a last example, one can wrap the date values in a pattern with the
pattern
argument. Note here that NA
values won't have the pattern
applied.
vec_fmt_date(str_vals, pattern = "Date: {x}")
#> [1] "Date: Monday, June 13, 2022" "Date: Friday, January 25, 2019"
#> [3] "Date: Monday, March 23, 2015" NA
14-10
Other vector formatting functions:
vec_fmt_bytes()
,
vec_fmt_currency()
,
vec_fmt_datetime()
,
vec_fmt_duration()
,
vec_fmt_engineering()
,
vec_fmt_fraction()
,
vec_fmt_integer()
,
vec_fmt_markdown()
,
vec_fmt_number()
,
vec_fmt_partsper()
,
vec_fmt_percent()
,
vec_fmt_scientific()
,
vec_fmt_time()