[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  as.Date: Date Conversion Functions to and from Character

##  Description

Functions to convert between character representations and objects of class `
"Date" ` representing calendar dates.

##  Usage

    
    
    as.Date(x, …)
    # S3 method for character
    as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
            optional = FALSE, …)
    # S3 method for numeric
    as.Date(x, origin, …)
    # S3 method for POSIXct
    as.Date(x, tz = "UTC", …)
    
    # S3 method for Date
    format(x, …)
    
    # S3 method for Date
    as.character(x, …)

##  Arguments

x

an object to be converted.

format

` [ character ](/link/character?package=base&version=3.6.2) ` string. If not
specified, it will try ` tryFormats ` one by one on the first non- ` NA `
element, and give an error if none works. Otherwise, the processing is via ` [
strptime ](/link/strptime?package=base&version=3.6.2) ` .

tryFormats

` [ character ](/link/character?package=base&version=3.6.2) ` vector of `
format ` strings to try if ` format ` is not specified.

optional

` [ logical ](/link/logical?package=base&version=3.6.2) ` indicating to return
` NA ` (instead of signalling an error) if the format guessing does not
succeed.

origin

a ` Date ` object, or something which can be coerced by ` as.Date(origin, …) `
to such an object.

tz

a time zone name.

…

further arguments to be passed from or to other methods, including ` format `
for ` as.character ` and ` as.Date ` methods.

##  Value

The ` format ` and ` as.character ` methods return a character vector
representing the date. ` NA ` dates are returned as ` NA_character_ ` .

The ` as.Date ` methods return an object of class ` " [ Date
](/link/Date?package=base&version=3.6.2) " ` .

##  Conversion from other Systems

Most systems record dates internally as the number of days since some origin,
but this is fraught with problems, including

  * Is the origin day 0 or day 1? As the ‘Examples’ show, Excel manages to use both choices for its two date systems. 

  * If the origin is far enough back, the designers may show their ignorance of calendar systems. For example, Excel's designer thought 1900 was a leap year (claiming to copy the error from earlier DOS spreadsheets), and Matlab's designer chose the non-existent date of ‘January 0, 0000’ (there is no such day), not specifying the calendar. (There is such a year in the ‘Gregorian’ calendar as used in ISO 8601:2004, but that does say that it is only to be used for years before 1582 with the agreement of the parties in information exchange.) 

The only safe procedure is to check the other systems values for known dates:
reports on the Internet (including R-help) are more often wrong than right.

##  Details

The usual vector re-cycling rules are applied to ` x ` and ` format ` so the
answer will be of length that of the longer of the vectors.

Locale-specific conversions to and from character strings are used where
appropriate and available. This affects the names of the days and months.

The ` as.Date ` methods accept character strings, factors, logical ` NA ` and
objects of classes ` " [ POSIXlt ](/link/POSIXlt?package=base&version=3.6.2) "
` and ` " [ POSIXct ](/link/POSIXct?package=base&version=3.6.2) " ` . (The
last is converted to days by ignoring the time after midnight in the
representation of the time in specified time zone, default UTC.) Also objects
of class ` "date" ` (from package  [ date
](/link/date?package=base&version=3.6.2&to=date%3Aas.date) ) and ` "dates" `
(from package  [ chron ](/link/chron?package=base&version=3.6.2&to=chron) ).
Character strings are processed as far as necessary for the format specified:
any trailing characters are ignored.

` as.Date ` will accept numeric data (the number of day