Convert string into date format in R
The as.Date()
function will convert a string into date format, and the format of the output will always be in yyyy-mm-dd
format in R (ISO 8601). The format
argument in the as.Date()
function is to specify the date format of the string input. I remember I initially thought it was specifying the output format, but it's the input format (you can change the output format with a subsequent format()
function, however this will convert it back to a string).
Your string looks to be in ddmmyyyy
(%d%m%Y
) format, this should be what you specify as the format argument in as.Date()
. Your format does not include hyphens, so the format
argument should also not include hyphens. Note that ddmmyyyy
, dd-mm-yyyy
, dd/mm/yyyy
, dd.mm.yyyy
are all different date formats, even though the day, month and year are in the same order, they would be converted to date with formats %d%m%Y
, %d-%m-%Y
, %d/%m/%Y
, and %d.%m.%Y
, respectively.
Further advice on working with dates and times is available in the relevant chapters of R for Data Science by Wickham & Grolemund and The R Cookbook by Teetor & Long.