philiporlando
philiporlando

Reputation: 1000

Convert unconventional datetime string to POSIXct in R

What format string argument of the as.POSIXct() function would allow me to coerce the following timestamp into POSIXct?

datetime <- "2018/02/08T23:58:33z"
datetime <- as.POSIXct(datetime, format = "%Y/%m/%d %H:%M:%S", tz = "UTC)

Desired result

2018-02-08 23:58:33

Upvotes: 1

Views: 172

Answers (2)

akrun
akrun

Reputation: 887148

Other option is to use anytime which parses automatically

anytime::anytime(datetime, tz = "UTC", asUTC = TRUE)
#[1] "2018-02-08 23:58:33 UTC"

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226192

Just put a literal "T" in the string to match (trailing characters are ignored by default anyway):

as.POSIXct(datetime, format = "%Y/%m/%dT%H:%M:%S", tz = "UTC")

Upvotes: 4

Related Questions