Date & Time splitting and conversion

I have column with "4/12/2016 7:21:00 AM" and I need to convert it into 2 columns:

  • date (in a date format): "2016-4-12"
  • time of the day in 24 hour format

You can do something like this

library(dplyr)

sample_df <- data.frame(
    time_stamp = c("4/12/2016 7:21:00 AM")
)

sample_df %>% 
    mutate(time_stamp = mdy_hms(time_stamp),
           date = as.Date(time_stamp),
           time = strftime(time_stamp, format="%H:%M:%S", tz = "UTC"))
#>            time_stamp       date     time
#> 1 2016-04-12 07:21:00 2016-04-12 07:21:00

Created on 2022-11-11 with reprex v2.0.2

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

Thank you very much fro your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.