0
votes

I have character field in the following format

df

sd
10/12/2017 6:12
10/12/2017 6:14

I want to convert it into date format so that I can extract time from it.

Now having read this, I don't want to use regex as I want to keep it more generic as the formats might change. So I wanted to convert it to a date format and use lubridate to extract required fields.

So i used the following :

d1 <- strptime(df$sd[1], "%m/%d/%Y%H:%M")

and it gives me the following result

d1
[1] "2017-10-12 IST"

whereas i was expecting the hours and mins to be included as well.

Also on trying to use

format(dmy_hms(df$sd), "%H:%M:%S")

I get that "All formats failed to parse. No formats found"

Any suggestions on this?

1
strptime(df$sd, "%m/%d/%Y%H:%M") works for me.Ronak Shah

1 Answers

2
votes

Here's the lubridate solution:

library(lubridate)

df <- data.frame(sd=c('10/12/2017 6:12','10/12/2017 6:14'),stringsAsFactors = F)

dmy_hm(df$sd)

#[1] "2017-12-10 06:12:00 UTC" "2017-12-10 06:14:00 UTC"