as.Date in R: How to Use as.Date() Function in R

To get today’s date in R, use the Sys.Date() function.

Sys.Date()

Output

[1] "2021-03-11"

To get the current date and time in R, use the date() function.

date()

Output

[1] "Thu Mar 11 16:59:16 2021"

Dates can be imported from character, numeric, POSIXlt, and POSIXct formats in R using the as.Date() method.

as.Date() in R

The as.Date() is a built-in R function that converts between character representations and objects of class “Date” representing the calendar dates. Rates are represented as the number of days since 1970-01-01, with negative values for earlier dates.

Syntax

as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
        optional = FALSE, tz = "UTC")

Parameters

x: It is an object to be converted.

format: It is a character string. If the format argument is 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.

tryFormats: It is a character vector of format strings to try if the format is not specified.

optional: It is a logical argument indicating to return NA (instead of signaling an error) if the format guessing fails.

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

tz: It is a time zone name.

Return Value

The as.Date() method returns the object of class “Date“.

Example

Let’s get the locale-specific date in R, using format() and Sys.Date() function.

dates <- format(Sys.Date(), "%a %b %d")
dates

Output

[1] "Thu Mar 11"

To convert a string to date in R, use the as.Date() function.

dates <- c("20nov1980", "20nov1991", "20nov1993", "10sep1993")
dt <- as.Date(dates, "%d%b%Y")
dt

Output

[1] "1980-11-20" "1991-11-20" "1993-11-20" "1993-09-10"

You can see that we used the as.Date( ) function to convert character data to dates. The format is as.Date(x, “format“), where x is the character data and format gives the appropriate format.

Convert date format using as.Date() function

If your dates are stored as characters, you have to provide them as.Date() function with your vector of dates and the format they are currently stored in, and “tadaaa“, is done.

Let’s define a vector of dates in one format, and then we will use the as.Date() function to convert it into another format.

dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
dt <- as.Date(dates, "%m/%d/%y")
dt

Output

[1] "1980-11-20" "1991-11-20" "2019-11-20" "1993-09-10"

How to Convert Date to Character in R

To convert a Date to a Character in R, use the as.Character( ) function.

dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
dt <- as.Date(dates, "%m/%d/%y")
strDates <- as.character(dt)
strDates

Output

[1] "1980-11-20"  "1991-11-20"  "2019-11-20"  "1993-09-10"

Passing an origin as an argument to the as.Date() function

We can pass the origin, which can be coerced by as.Date(origin) to such an object.

Let’s importing Dates from the Numeric Format.

as.Date(34291, origin = "1900-01-01")

Output

[1] "1993-11-20"

In the output, you can see that the dates in the ISO 8601 international standard format %Y-%m-%d.

How to Change Date Formats in R

To change the date formats in R, use the format() function. For example, if you want to get the date than the standard %Y-%m-%d, use the format() function from the base package.

dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
dt <- as.Date(dates, "%m/%d/%y")
dt
cat("After formatting a date in other format", "\n")
fmt <- format(dt, "%a %b %d")
fmt

Output

[1] "1980-11-20" "1991-11-20" "2019-11-20" "1993-09-10"
After formatting a date in other format
[1] "Thu Nov 20" "Wed Nov 20" "Wed Nov 20" "Fri Sep 10"

That is it for as.Date() function in R tutorial.

Leave a Comment