Search code examples
rdatetimelubridate

How can I parse_date_time with letters and timezones?


I have a pretty standard looking date time as a character. I'm trouble parsing it into the datetime because of the letters and redundancies.

How can I parse this as a datetime?

lubridate::parse_date_time("Fri Feb 05 09:58:38 PST 2021", )

Solution

  • You can specify all the formats by directly referring to the documentation of the function parse_date_time(). You can find it here. The tricky thing here is that you need to specify the time zone via the argument tz and not directly in the argument orders.

    Below is the code.

    library(lubridate)
    #> 
    #> Attaching package: 'lubridate'
    #> The following objects are masked from 'package:base':
    #> 
    #>     date, intersect, setdiff, union
    newdate = parse_date_time("Fri Feb 05 09:58:38 PST 2021", 
                              "%a %b %d %H:%M:%S %Y", 
                              tz = "America/Los_Angeles" )
    newdate
    #> [1] "2021-02-05 09:58:38 PST"
    

    Created on 2021-02-16 by the reprex package (v0.3.0)

    Does this work for you?