lubridate mdy format issue

Advertisements

I have a date column whose date values are something like this:

date = c("1/6/2022", "1/6/2022", "1/19/2022", "1/20/2022")

When I try to convert it to new date column using lubridate::mdy I get:

library(lubridate)

date_new = lubridate::mdy(date)

print(date_new)

[1] "2022-01-06" "2022-01-06" "2022-01-19" "2022-01-20"

# Desired output (mm-dd-yyyy or mm/dd/yyyy): 
[1] "01-06-2022" "01-06-2022" "01-19-2022" "01-20-2022"

How can I get the desired output using lubridate?

>Solution :

We can use format

format(lubridate::mdy(date), '%m-%d-%Y')
[1] "01-06-2022" "01-06-2022" "01-19-2022" "01-20-2022"

Leave a ReplyCancel reply