Search code examples
rrmongo

Convert Mongo Datestamp to R readable date format.


I pulled data into R from MongoDB and my dates are in the format string "Thu May 08 01:00:00 EAT 2008". I want to change them to format "%a %b %d %H:%M:%S" readable by R and been banging my head so far.

I came around using DF$createdAt <- as.Date(DF$createdAt,format="%a %b %d %H:%M:%S") but this turns all the years to current year 2013.

Anyone know what I'm doing wrong?

    createAt
     Thu May 08 01:00:00 EAT 2008
      Tue May 13 01:00:00 EAT 2008
      Tue May 13 01:00:00 EAT 2008
      Thu May 15 01:00:00 EAT 2008
     Mon May 19 01:00:00 EAT 2008

Solution

  • You need to include %Y for the four-digit year.

    fmt <- "%a %b %d %H:%M:%S EAT %Y"
    as.Date("Thu May 08 01:00:00 EAT 2008", fmt)
    ## [1] "2008-05-08"
    strptime("Thu May 08 01:00:00 EAT 2008", fmt)
    ## [1] "2008-05-08 01:00:00"
    

    When you come to print the values, use strftime or format, and specify how you would like the dates to look.