Home > database >  Conversion of character into datetime in R
Conversion of character into datetime in R

Time:07-06

How to convert a column of character "2020-08-12T04:31:41Z" into datetime. I have tried as.POSIXct() function, but, when I give value to format it returns NA.

CodePudding user response:

You can convert the time using utctime from the anytime package or as_datetime from lubridate like this:

df <- data.frame(date = "2020-08-12T04:31:41Z")

library(anytime)
utctime(df$date, tz="UTC")
#> [1] "2020-08-12 04:31:41 UTC"
library(lubridate)
as_datetime(df$date)
#> [1] "2020-08-12 04:31:41 UTC"

Created on 2022-07-06 by the reprex package (v2.0.1)

CodePudding user response:

You can use strptime which creates a datetime object from the given string

strptime("2020-08-12T04:31:41Z", tz = "UTC", "%Y-%m-%dT%H:%M:%OSZ")
  • Related