0

How can I split "16/03/2021 08:32" into time only with R?

I tried using "format(as.POSIXct(bike_rides1$started_at), format = "%H:%M")" and it returned 00:00, I also tried "format(as.POSIXct(bike_rides1$started_at), format = "%H:%M:%S")" and it returned 00:00:00 as well.

Nad Pat
  • 3,129
  • 3
  • 10
  • 20
OLUFUNKE
  • 23
  • 2
  • The date part of your string is in a non standard format. See this for a starting point https://stackoverflow.com/questions/32261872/convert-date-string-in-format-mon-day-year-time-am-pm-to-posixlt-format-in-r – Dave2e Mar 22 '22 at 02:24
  • `substr("16/03/2021 08:32", 12, 16)`? – zephryl Mar 22 '22 at 02:58
  • `sub("^\\S+\\s+", '', "16/03/2021 08:32")` – Nad Pat Mar 22 '22 at 03:28

1 Answers1

0

You can use lubridate::dmy_hm() (day-month-year hour-minutes) to get your dates into a standard format, and then your format would work:

lubridate::dmy_hm("16/03/2021 08:32") |>
  format(format = "%H:%M")

#> [1] "08:32"
jpiversen
  • 3,062
  • 1
  • 8
  • 12