Reputation: 2477
I've got some unusually formatted strings that I need to get into datetime objects, and I can't find what I need from the strptime function documentation. Examples of strings that I need formatted are:
4/16/2018 0:00:00
8/30/2019 14:35:00
11/15/2017 8:15:10
I can't find any specification that matches these kinds of strings that I can use as the format
in strptime
. Am I going to have to format the strings first?
Upvotes: 1
Views: 54
Reputation: 7730
Seem to be quite common strings for strptime. Or was there another problem?
x <- "4/16/2018 0:00:01"
strptime(x ,"%m/%d/%Y %H:%M:%S")
Upvotes: 2
Reputation: 1261
I think this is enough:
library(lubridate)
dates = c('4/16/2018 0:00:00', '8/30/2019 14:35:00', '11/15/2017 8:15:10')
dates = mdy_hms(dates)
dates
Output:
[1] "2018-04-16 00:00:00 UTC" "2019-08-30 14:35:00 UTC" "2017-11-15 08:15:10 UTC"
Upvotes: 2
Reputation: 887971
We can use the format "%m/%d/%Y %H:%M:%S"
to convert to DateTime
as.POSIXct(str1, format = "%m/%d/%Y %H:%M:%S")
#[1] "2018-04-16 00:00:00 CDT" "2019-08-30 14:35:00 CDT" "2017-11-15 08:15:10 CST"
str1 <- c("4/16/2018 0:00:00", "8/30/2019 14:35:00", "11/15/2017 8:15:10")
Upvotes: 3