Search code examples
rdatedatetimedatetime-formatlubridate

Parsing string/ISO 8601 Date


I am looking for a way to parse multiple string formats to dates/datetime. the as_datetime() functions works very well for my purposes for the first 3 below, but not the fourth. Any suggestions?

library(lubridate)
as_datetime("20200603", tz = "America/New_York")
as_datetime("20200603231413", tz = "America/New_York")
as_datetime("20200603231413-0400", tz = "America/New_York")
as_datetime("20200528203000+0000", tz = "America/New_York")


>   as_datetime("20200603", tz = "America/New_York")
[1] "2020-06-03 EDT"

>   as_datetime("20200603231413", tz = "America/New_York")
[1] "2020-06-03 23:14:13 EDT"

>   as_datetime("20200603231413-0400", tz = "America/New_York")
Date in ISO8601 format; converting timezone from UTC to "America/New_York".
[1] "2020-06-03 23:14:13 EDT"  

>   as_datetime("20200528203000+0000", tz = "America/New_York")
[1] NA
Warning message:
All formats failed to parse. No formats found. 

>   as_datetime("20200528203116+0000", tz = "America/New_York")
[1] NA
Warning message:
All formats failed to parse. No formats found. 

Solution

  • You can provide more formats with format=, and in this case add %z, which ?strptime defines as

         '%z' Signed offset in hours and minutes from UTC, so '-0800' is 8
              hours behind UTC. Values up to '+1400' are accepted.
              (Standard only for output.  For input R currently supports it
              on all platforms.)
    
    library(lubridate)
    as_datetime("20200603231413-0400", format = "%Y%m%d%H%M%S%z", tz = "America/New_York")
    # [1] "2020-06-03 23:14:13 EDT"
    as_datetime("20200628203000+0000", format = "%Y%m%d%H%M%S%z", tz = "America/New_York")
    # [1] "2020-06-28 16:30:00 EDT"
    as_datetime("20200528203116+0000", format = "%Y%m%d%H%M%S%z", tz = "America/New_York")
    # [1] "2020-05-28 16:31:16 EDT"