6

I'm working with a date format of YYYY-mm-ddTHH:MM:SS.000Z (2014-02-05T08:45:01.326Z) or that has a separator T that separates the date from the time, and time indicator Z or "Zulu time" (UTC). I'm trying to store the timestamp as a class POSIXct using the following function:

timestamp <- as.POSIXct(strptime(as.character(data$Time), tz = "UTC", "%Y-%m-%d %H:%M:%S"))

at the moment I'm getting NA's. If anyone has some advice on how I can incorporate the 'T' and 'Z' indicators in my conversion I will highly appreciate it.

Carmen
  • 117
  • 1
  • 7
  • Perhaps, a way could be to replace all T's and Z's and then convert. Possibly, something like `strptime(gsub("T|Z", " ", data$time), ...)` – alexis_laz Feb 25 '14 at 09:12

1 Answers1

10

You can include the characters in your format string:

d <- "2014-02-05T08:45:01.326Z"
timestamp <- strptime(d, tz = "UTC", "%Y-%m-%dT%H:%M:%OSZ")

Note that here %OS is used instead of %S because you have fractional seconds.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • Thank you very much! It worked. I tried to add the T and Z indicators but I think it was because I left out the %OS that got me confused! Thanks again! – Carmen Feb 25 '14 at 09:20