You would use as.Date()
to convert between dates saved as character and Date
objects.
If you want to change the format of a Date
object, you can use format()
.
You have specified "2016-month-day"
as the desired format of the dates in the question, but in the code you provide you are using "%d/%b/%Y"
. The way this works is: the %
indicates that the next character will be a conversion specification, everything else (e.g. (-
or /
) will be used for finding / adding delimiter to the date representation. (see ?strptime
for details).
So in your case, just use
dates <- format(dli$Dates, format = "%Y-%b-%d")
to get the result specified in the text of the question:
[1] "2016-Jan-01" "2016-Jan-02" "2016-Jan-03" "2016-Jan-04" "2016-Jan-05"
or this:
dates <- format(dli$Dates, format = "%Y/%b/%d")
to get what you have used in the code snipped:
[1] "2016/Jan/01" "2016/Jan/02" "2016/Jan/03" "2016/Jan/04" "2016/Jan/05"