Search code examples
rdate-format

Standardizing the date format using R


I am having trouble standardizing the Date format to be dd-mm-YYYY, This is my current code

Dataset

         date
1  23/07/2020
2 22-Jul-2020

Current Output

df$date<-as.Date(df$date)
df$date = format(df$date, "%d-%b-%Y")

         date
1 20-Jul-0022
2        <NA>

Desired Output

         date
1 23-Jul-2020
2 22-Jul-2020

Solution

  • You can try this way

    library(lubridate)
    df$date <- dmy(df$date)
    df$date <- format(df$date, format = "%d-%b-%Y")
    
    #         date
    # 1 23-Jul-2020
    # 2 22-Jul-2020
    

    Data

     df <- read.table(text = "date
        1  23/07/2020
        2 22-Jul-2020", header = TRUE)