Skip to content

[ lubridate ](../index.html) 1.9.2

  * [ Get started ](../articles/lubridate.html)
  * [ Reference ](../reference/index.html)
  * News 

######  Releases

[ Version 1.7.0 ](https://www.tidyverse.org/blog/2017/11/lubridate-1.7.0/) [
Version 1.6.0 ](https://www.rstudio.com/blog/lubridate-1-6-0/) [ Version 1.2.0
](https://www.rstudio.com/blog/lubridate-1-2-0-now-on-cran/)

[ Changelog ](../news/index.html)

  * [ ](https://github.com/tidyverse/lubridate/)

![](../logo.png)

#  Convert an object to a date or date-time

Source: [ ` R/coercion.r `
](https://github.com/tidyverse/lubridate/blob/HEAD/R/coercion.r)

` as_date.Rd `

Convert an object to a date or date-time

##  Usage

    
    
    as_date(x, ...)
    
    # S4 method for ANY
    as_date(x, ...)
    
    # S4 method for POSIXt
    as_date(x, tz = NULL)
    
    # S4 method for numeric
    as_date(x, origin = lubridate::[origin](origin.html))
    
    # S4 method for character
    as_date(x, tz = NULL, format = NULL)
    
    as_datetime(x, ...)
    
    # S4 method for ANY
    as_datetime(x, tz = lubridate::[tz](tz.html)(x))
    
    # S4 method for POSIXt
    as_datetime(x, tz = lubridate::[tz](tz.html)(x))
    
    # S4 method for numeric
    as_datetime(x, origin = lubridate::[origin](origin.html), tz = "UTC")
    
    # S4 method for character
    as_datetime(x, tz = "UTC", format = NULL)
    
    # S4 method for Date
    as_datetime(x, tz = "UTC")

##  Arguments

x

    

a vector of [ POSIXt ](https://rdrr.io/r/base/DateTimeClasses.html) , numeric
or character objects

...

    

further arguments to be passed to specific methods (see above).

tz

    

a time zone name (default: time zone of the POSIXt object ` x ` ). See ` [
OlsonNames() ](https://rdrr.io/r/base/timezones.html) ` .

origin

    

a Date object, or something which can be coerced by ` as.Date(origin, ...) `
to such an object (default: the Unix epoch of "1970-01-01"). Note that in this
instance, ` x ` is assumed to reflect the number of days since ` origin ` at `
"UTC" ` .

format

    

format argument for character methods. When supplied parsing is performed by `
parse_date_time(x, orders = formats, exact = TRUE) ` . Thus, multiple formats
are supported and are tried in turn.

##  Value

a vector of [ Date ](date_utils.html) objects corresponding to ` x ` .

##  Compare to base R

These are drop in replacements for ` [ as.Date()
](https://rdrr.io/r/base/as.Date.html) ` and ` [ as.POSIXct()
](https://rdrr.io/r/base/as.POSIXlt.html) ` , with a few tweaks to make them
work more intuitively.

  * Called on a ` POSIXct ` object, ` as_date() ` uses the tzone attribute of the object to return the same date as indicated by the printed representation of the object. This differs from as.Date, which ignores the attribute and uses only the tz argument to ` [ as.Date() ](https://rdrr.io/r/base/as.Date.html) ` ("UTC" by default). 

  * Both functions provide a default origin argument for numeric vectors. 

  * Both functions will generate NAs for invalid date format. Valid formats are those described by ISO8601 standard. A warning message will provide a count of the elements that were not converted. 

  * ` as_datetime() ` defaults to using UTC. 

##  Examples

    
    
    dt_utc <- [ymd_hms](ymd_hms.html)("2010-08-03 00:50:50")
    dt_europe <- [ymd_hms](ymd_hms.html)("2010-08-03 00:50:50", tz = "Europe/London")
    [c](https://rdrr.io/r/base/c.html)(as_date(dt_utc), [as.Date](https://rdrr.io/r/base/as.Date.html)(dt_utc))
    #> [1] "2010-08-03" "2010-08-03"
    [c](https://rdrr.io/r/base/c.html)(as_date(dt_europe), [as.Date](https://rdrr.io/r/base/as.Date.html)(dt_europe))
    #> [1] "2010-08-03" "2010-08-02"
    ## need not supply origin
    as_date(10)
    #> [1] "1970-01-11"
    ## Will replace invalid date format with NA
    dt_wrong <- [c](https://rdrr.io/r/base/c.html)("2009-09-29", "2012-11-29", "2015-29-12")
    as_date(dt_wrong)
    #> Warning:  1 failed to parse.
    #> [1] "2009