as_date_time | R Documentation |
as_date_time()
is a generic function that converts its input to a date-time
(POSIXct).
There are methods for converting dates (Date), calendars, time points, and zoned-times to date-times.
For converting to a date, see as_date()
.
as_date_time(x, ...)
## S3 method for class 'POSIXt'
as_date_time(x, ...)
## S3 method for class 'Date'
as_date_time(x, zone, ..., nonexistent = NULL, ambiguous = NULL)
## S3 method for class 'clock_calendar'
as_date_time(x, zone, ..., nonexistent = NULL, ambiguous = NULL)
## S3 method for class 'clock_sys_time'
as_date_time(x, zone, ...)
## S3 method for class 'clock_naive_time'
as_date_time(x, zone, ..., nonexistent = NULL, ambiguous = NULL)
## S3 method for class 'clock_zoned_time'
as_date_time(x, ...)
x |
A vector. |
... |
These dots are for future extensions and must be empty. |
zone |
The zone to convert to. |
nonexistent |
One of the following nonexistent time resolution strategies, allowed to be either length 1, or the same length as the input:
Using either If If |
ambiguous |
One of the following ambiguous time resolution strategies, allowed to be either length 1, or the same length as the input:
Alternatively, Finally, If If |
Note that clock always assumes that R's Date class is naive, so converting
a Date to a POSIXct will always attempt to retain the printed year, month,
and day. Where possible, the resulting time will be at midnight (00:00:00
),
but in some rare cases this is not possible due to daylight saving time. If
that issue ever arises, an error will be thrown, which can be resolved by
explicitly supplying nonexistent
or ambiguous
.
This is not a drop-in replacement for as.POSIXct()
, as it only converts a
limited set of types to POSIXct. For parsing characters as date-times, see
date_time_parse()
. For converting numerics to date-times, see
vctrs::new_datetime()
or continue to use as.POSIXct()
.
A date-time with the same length as x
.
x <- as.Date("2019-01-01")
# `as.POSIXct()` will always treat Date as UTC, but will show the result
# of the conversion in your system time zone, which can be somewhat confusing
if (rlang::is_installed("withr")) {
withr::with_timezone("UTC", print(as.POSIXct(x)))
withr::with_timezone("Europe/Paris", print(as.POSIXct(x)))
withr::with_timezone("America/New_York", print(as.POSIXct(x)))
}
# `as_date_time()` will treat Date as naive, which means that the original
# printed date will attempt to be kept wherever possible, no matter the
# time zone. The time will be set to midnight.
as_date_time(x, "UTC")
as_date_time(x, "Europe/Paris")
as_date_time(x, "America/New_York")
# In some rare cases, this is not possible.
# For example, in Asia/Beirut, there was a DST gap from
# 2021-03-27 23:59:59 -> 2021-03-28 01:00:00,
# skipping the 0th hour entirely.
x <- as.Date("2021-03-28")
try(as_date_time(x, "Asia/Beirut"))
# To resolve this, set a `nonexistent` time resolution strategy
as_date_time(x, "Asia/Beirut", nonexistent = "roll-forward")
# You can also convert to date-time from other clock types
as_date_time(year_month_day(2019, 2, 3, 03), "America/New_York")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.