alily alily - 4 months ago 19
R Question

Convert variable with custom value into date time format in R

I have a variable with values of form "

(31-Aug-2010) 14:00:41.386
" how can i convert it into Date time format in R.

I did try:

as.POSIXct(Date.Time,format="(%d-%m-%Y) %H:%M:%OS3")

my_date = strptime(as.character(Date.Time),"(%d-%m-%Y) %H:%M:%OS3")

mdy_hms(Date.Time,format= "(%d-%m-%Y) %H:%M:%OS3")

None of them worked for me.All the above methods gave NA's with warning message "All formats failed to parse.No formats found."
Can anyone suggest me better way.
I did try looking at answers for other similar questions on this website but couldn't find suitable answer,so posted a new question.


As the month is in three letter format, in the format, it should be %b and not %m. It is well documented in strptime

as.POSIXct(str1, format = "(%d-%b-%Y) %H:%M:%OS")
#[1] "2010-08-31 14:00:41 IST"

If we change the options, it will print the milliseconds

op <- options(digits.secs=3)
as.POSIXct(str1, format = "(%d-%b-%Y) %H:%M:%OS")
#[1] "2010-08-31 14:00:41.385 IST"


str1 <- "(31-Aug-2010) 14:00:41.386"