One of the most common date tasks you will need to do is to convert a string to date. R provides many standard options for doing this. In this article, we will learn how to convert a string to date.
To convert a string to date, you can use the as.Date
method if you string conforms to a build in standard format.
as.Date("2021-04-21")
# [1] "2021-04-21"
However, if your date does not confirm to a build in stadard, you will receive and error, or receive some odd results
as.Date("04/12/2021")
# [1] "0004-12-20"
In this cases, you will need to specify the date format using the format
named parameter.
as.Date("4/12/2021", format = "%m/%d/%Y")
# [1] "2021-04-12"