Parse ISO8601 datetime format with UTC offset

Right now I jump through all sorts of hoops to parse a specific datetime format with offset from UTC. My guess is that one of you might know an easy way of parsing the datetime format :slight_smile: It's written like:

dt <- "2019-09-17T01:00:00+02:00"

I can get transform it to a POSIXct vaue like this.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
DateTime <- ymd_hms("2019-09-17T01:00:00+02:00", tz = "CET")
#> Date in ISO8601 format; converting timezone from UTC to "CET".
DateTime
#> [1] "2019-09-17 01:00:00 CEST"

Created on 2019-09-16 by the reprex package (v0.2.1)
I guessed the time zone you want to use. If you leave out the time zone, the returned value will be the UTC equivalent "2019-09-16 23:00:00 UTC"

3 Likes

One of the examples from the lubridate documentation illustrates how the parser can handle several variations on time zone offset formats, too:

## ** different formats for ISO8601 timezone offset **
ymd_hms(c("2013-01-24 19:39:07.880-0600",
"2013-01-24 19:39:07.880", "2013-01-24 19:39:07.880-06:00",
"2013-01-24 19:39:07.880-06", "2013-01-24 19:39:07.880Z"))
#> [1] "2013-01-25 01:39:07.88 UTC" "2013-01-24 19:39:07.88 UTC"
#> [3] "2013-01-25 01:39:07.88 UTC" "2013-01-25 01:39:07.88 UTC"
#> [5] "2013-01-24 19:39:07.88 UTC"
1 Like

Thanks. I had no idea the lubridate functions where this versatile.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.