Learn R Programming

clock (version 0.7.1)

as_date: Convert to a date

Description

as_date() is a generic function that converts its input to a date (Date).

There are methods for converting date-times (POSIXct), calendars, time points, and zoned-times to dates.

For converting to a date-time, see as_date_time().

Usage

as_date(x, ...)

# S3 method for Date as_date(x, ...)

# S3 method for POSIXt as_date(x, ...)

# S3 method for clock_calendar as_date(x, ...)

# S3 method for clock_time_point as_date(x, ...)

# S3 method for clock_zoned_time as_date(x, ...)

Value

A date with the same length as x.

Arguments

x

[vector]

A vector.

...

These dots are for future extensions and must be empty.

Details

Note that clock always assumes that R's Date class is naive, so converting a POSIXct to a Date will always retain the printed year, month, and day value.

This is not a drop-in replacement for as.Date(), as it only converts a limited set of types to Date. For parsing characters as dates, see date_parse(). For converting numerics to dates, see vctrs::new_date() or continue to use as.Date().

Examples

Run this code
x <- date_time_parse("2019-01-01 23:02:03", "America/New_York")

# R's `as.Date.POSIXct()` method defaults to changing the printed time
# to UTC before converting, which can result in odd conversions like this:
as.Date(x)

# `as_date()` will never change the printed time before converting
as_date(x)

# Can also convert from other clock types
as_date(year_month_day(2019, 2, 5))

Run the code above in your browser using DataLab