1

Is there a way for me to parse the following string in a date time format in R which includes not just the date but also the time zone (+08:00). The time zone here is actually not that important but would be good to include. I've tried using as.Date and as.POSIXct but both throws me NA. My main aim of converting into date time is to be able to sort them by time and also filter the data frame by time when necessary.

01 Jul 2019 12:56:43 +08:00

Would be glad to provide more information if required.

0

3 Answers 3

4

The package lubridate is a godd choice to parse (and work with) dates:

library(lubridate)

dmy_hms("01 Jul 2019 12:56:43 +08:00")
0
4

Use the dmy_hms function from lubridate package as others have suggested but be sure to specify the local timezone you desire to use via the tz argument.

lurbidate::dmy_hms("01 Jul 2019 12:56:43 +08:00", tz = "America/Chicago")

R> dmy_hms(date, tz = "America/Chicago")
Date in ISO8601 format; converting timezone from UTC to "America/Chicago".
[1] "2019-06-30 23:56:43 CDT"

For an extensive list of available timezones, execute

OlsonNames(tzdir = NULL)

R> OlsonNames(tzdir = NULL)
  [1] "Africa/Abidjan"                   "Africa/Accra"                     "Africa/Addis_Ababa"              
  [4] "Africa/Algiers"                   "Africa/Asmara"                    "Africa/Asmera"                   
  [7] "Africa/Bamako"                    "Africa/Bangui"                    "Africa/Banjul"                   
 [10] "Africa/Bissau"                    "Africa/Blantyre"                  "Africa/Brazzaville"              
 [13] "Africa/Bujumbura"                 "Africa/Cairo"                     "Africa/Casablanca"               
 [16] "Africa/Ceuta"                     "Africa/Conakry"                   "Africa/Dakar"                    
 [19] "Africa/Dar_es_Salaam"             "Africa/Djibouti"                  "Africa/Douala"                   
 [22] "Africa/El_Aaiun"                  "Africa/Freetown"                  "Africa/Gaborone"                 
 [25] "Africa/Harare"                    "Africa/Johannesburg"              "Africa/Juba"                     
 [28] "Africa/Kampala"                   "Africa/Khartoum"                  "Africa/Kigali" 
...
1
  • 1
    That's a good hint! However this will only effect display; results of calculations will be correct in any case.
    – AEF
    Commented Jul 2, 2019 at 5:21
0

Should be possible with base R as.POSIXct and strptime as well

as.POSIXct("01 Jul 2019 12:56:43 +08:00", format = "%d %b %Y %T")

OR

strptime("01 Jul 2019 12:56:43 +08:00", format = "%d %b %Y %T")
3
  • Need to add %z and remove colon from offset: as.POSIXct(sub(":(..)$", "\\1", "01 Jul 2019 12:56:43 +09:00"), format = "%d %b %Y %T %z") Commented Jul 1, 2019 at 13:24
  • @G.Grothendieck I am surprised but this still works for me as.POSIXct("01 Jul 2019 12:56:43 +08:00", format = "%d %b %Y %T") #[1] "2019-07-01 12:56:43 +08"
    – Ronak Shah
    Commented Jul 1, 2019 at 13:29
  • With the code in the answer, on Windows R 3.5.3 if I change the +08:00 to +09:00 I still get the same result. Commented Jul 1, 2019 at 13:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.