Search code examples
rdate-format

Convert date format from dd-mm-yyyy to dd/mm/yyyy


I would like convert my date format from dd-mm-yyyy to dd/mm/yyyy

Data:

         date
1 22-Jul-2020

Current code:

format(as.Date(df$date, '%d:%m:%Y'), '%d/%m/%Y' )
[1] NA NA

Desired Output:

         date
1 22/07/2020

Solution

  • The format in as.Date should match the input format. It is %d followed by -, then abbrevation for month (%b) followed by - and 4 digit year (%Y)

    df$date <- format(as.Date(df$date, '%d-%b-%Y'), '%d/%m/%Y' )
    df$date
    #[1] "22/07/2020"
    

    data

    df <- structure(list(date = "22-Jul-2020"), class = "data.frame", row.names = "1")