Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

Viewed 5284

I've got a tbl_dfwith two columns StartTime and StopTime. Both are dttm.

I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d".

I've tried

data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime))

But it returns

Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found.

How can I do it?

Please, don't send other questions that don't use lubridate package.

Thanks

2 Answers

I once had a problem where the date-format was like: "2019-12-06T09:27:14".

For me ymd_hms did not work and I did not find a fitting parser, so the solution was first to string_replace the uneccessary part and then to parse to date as usual.. (and discard the original column if needed)

 your_df %>%mutate(date_published = 
                   str_replace(article.date_published,T\\d\\d\\:\\d\\d\\:\\d\\d', ''), 
                   date_published= ymd(date_published))%>%
           select(-contains('article.date'))
Related