R Language The Date class Dates

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To coerce a variable to a date use the as.Date() function.

> x <- as.Date("2016-8-23")
> x
[1] "2016-08-23"
> class(x)
[1] "Date"

The as.Date() function allows you to provide a format argument. The default is %Y-%m-%d, which is Year-month-day.

> as.Date("23-8-2016", format="%d-%m-%Y") # To read in an European-style date
[1] "2016-08-23"

The format string can be placed either within a pair of single quotes or double quotes. Dates are usually expressed in a variety of forms such as: "d-m-yy" or "d-m-YYYY" or "m-d-yy" or "m-d-YYYY" or "YYYY-m-d" or "YYYY-d-m". These formats can also be expressed by replacing "-" by "/". Furher, dates are also expressed in the forms, say, "Nov 6, 1986" or "November 6, 1986" or "6 Nov, 1986" or "6 November, 1986" and so on. The as.Date() function accepts all such character strings and when we mention the appropriate format of the string, it always outputs the date in the form "YYYY-m-d".

Suppose we have a date string "9-6-1962" in the format "%d-%m-%Y".

#
# It tries to interprets the string as YYYY-m-d
#
> as.Date("9-6-1962")
[1] "0009-06-19"       #interprets as "%Y-%m-%d"
> 
as.Date("9/6/1962")
[1] "0009-06-19"       #again interprets as "%Y-%m-%d"
>
# It has no problem in understanding, if the date is in form  YYYY-m-d or YYYY/m/d
#
> as.Date("1962-6-9")
[1] "1962-06-09"        # no problem
> as.Date("1962/6/9")
[1] "1962-06-09"        # no problem
> 

By specifying the correct format of the input string, we can get the desired results. We use the following codes for specifying the formats to the as.Date() function.

Format CodeMeaning
%dday
%mmonth
%yyear in 2-digits
%Yyear in 4-digits
%babbreviated month in 3 chars
%Bfull name of the month

Consider the following example specifying the format parameter:

> as.Date("9-6-1962",format="%d-%m-%Y")
[1] "1962-06-09"
>

The parameter name format can be omitted.

> as.Date("9-6-1962", "%d-%m-%Y")
[1] "1962-06-09"
>

Some times, names of the months abbreviated to the first three characters are used in the writing the dates. In which case we use the format specifier %b.

> as.Date("6Nov1962","%d%b%Y")
[1] "1962-11-06"
>

Note that, there are no either '-' or '/' or white spaces between the members in the date string. The format string should exactly match that input string. Consider the following example:

> as.Date("6 Nov, 1962","%d %b, %Y")
[1] "1962-11-06"
>

Note that, there is a comma in the date string and hence a comma in the format specification too. If comma is omitted in the format string, it results in an NA. An example usage of %B format specifier is as follows:

> as.Date("October 12, 2016", "%B %d, %Y")
[1] "2016-10-12"
>
> as.Date("12 October, 2016", "%d %B, %Y")
[1] "2016-10-12"
> 

%y format is system specific and hence, should be used with caution. Other parameters used with this function are origin and tz( time zone).



Got any R Language Question?