0

I have a dataframe in r which consists of a column with the following DateTime format with character class.

dt<-2021-03-01 06:10:31.346

I need to convert it into the following format with datetime class.

2021-03-01 06:10:31

I have tried the following solution but it didn't work.

Df$Dt<- as.POSIXct(Df$Dt,format="%Y-%m-%dT%H:%M:%S")
user9211845
  • 131
  • 1
  • 12

1 Answers1

1

The T is not needed as it was not present in the original string

as.POSIXct(dt, format="%Y-%m-%d %H:%M:%S")
#[1] "2021-03-01 06:10:31 CST"

If we want to take care of the microseconds

as.POSIXct(dt, format="%Y-%m-%d %I:%M:%OS")

Also, the format is not really needed here as the input is in the accepted format for as.POSIXct

as.POSIXct(dt)
#[1] "2021-03-01 06:10:31 CST"

data

dt <- '2021-03-01 06:10:31.346'
akrun
  • 874,273
  • 37
  • 540
  • 662