View source: R/gregorian-year-month-day.R
year_month_day_parse | R Documentation |
year_month_day_parse()
parses strings into a year-month-day.
The default options assume x
should be parsed at day precision, using a
format
string of "%Y-%m-%d"
.
If a more precise precision than day is used, then time components will also
be parsed. The default format separates date and time components by a "T"
and the time components by a ":"
. For example, setting the precision to
"second"
will use a default format of "%Y-%m-%dT%H:%M:%S"
. This is
aligned with the format()
method for year-month-day, and with the RFC 3339
standard.
year_month_day_parse(
x,
...,
format = NULL,
precision = "day",
locale = clock_locale()
)
x |
A character vector to parse. |
... |
These dots are for future extensions and must be empty. |
format |
A format string. A combination of the following commands, or A vector of multiple format strings can be supplied. They will be tried in the order they are provided. Year
Month
Day
Day of the week
ISO 8601 week-based year
Week of the year
Day of the year
Date
Time of day
Time zone
Miscellaneous
|
precision |
A precision for the resulting year-month-day. One of:
Setting the |
locale |
A locale object created by |
year_month_day_parse()
completely ignores the %z
and %Z
commands.
A year-month-day calendar vector. If a parsing fails, NA
is
returned.
It is highly recommended to parse all of the information in the date-time
string into a type at least as precise as the string. For example, if your
string has fractional seconds, but you only require seconds, specify a
sub-second precision
, then round to seconds manually using whatever
convention is appropriate for your use case. Parsing such a string directly
into a second precision result is ambiguous and undefined, and is unlikely to
work as you might expect.
x <- "2019-01-01"
# Default parses at day precision
year_month_day_parse(x)
# Can parse at less precise precisions too
year_month_day_parse(x, precision = "month")
year_month_day_parse(x, precision = "year")
# Even invalid dates can be round-tripped through format<->parse calls
invalid <- year_month_day(2019, 2, 30)
year_month_day_parse(format(invalid))
# Can parse with time of day
year_month_day_parse(
"2019-01-30T02:30:00.123456789",
precision = "nanosecond"
)
# Can parse using multiple format strings, which will be tried
# in the order they are provided
x <- c("2019-01-01", "2020-01-01", "2021/2/3")
formats <- c("%Y-%m-%d", "%Y/%m/%d")
year_month_day_parse(x, format = formats)
# Can parse using other format tokens as well
year_month_day_parse(
"January, 2019",
format = "%B, %Y",
precision = "month"
)
# Parsing a French year-month-day
year_month_day_parse(
"octobre 1, 2000",
format = "%B %d, %Y",
locale = clock_locale("fr")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.