I am trying to convert a character field into a date field for which the options are to either use strptime
function or as.Date
function. Here is two r
I would highly recommend lubridate
for date conversions
library(lubridate)
mdy("5/13/2015")
# [1] "2015-05-13"
Your date format string should use a capital "Y", which captures the full date with century, such as "2015". Lowercase "y" is used to capture the year in two digits, as when "2015" is written in shorthand as "15". In your original cases, the strptime
and as.Date
functions incorrectly interpret the "20" in "2015" as the 2-digit year. The corrected version:
strptime(c("5/13/2015"),"%m/%d/%Y")
"2015-05-13 EDT"