Displaying dates to the user is a common task you will do repeatidly. Like most languages, R provides us with date formatting options. In this article, we will learn how to convert a date to a string.
To convert a date to a string, we have two methods. We can use the as.character
method or the format
method. The format method allows use to sepecify a date format and is more useful in general.
today = Sys.Date()
as.character(today)
# [1] "2021-04-24"
We see that the as.character
method givues us a default formatted date string. The format
method does the same thing unless we pass a format string to the named parameter.
today = Sys.Date()
format(today)
# [1] "2021-04-24"
format(today, format = "%m/%d/%Y")
# [1] "04/24/2021"