Convert between date formats in R?

I have data.frame (CSV originally) in R with dates in the following three formats:

2011-06-02T17:16:05Z 2012-06-02T17:16:05-07:00 6/2/11 17:16:05 

which is year-month-day-time. I don’t quite understand what -07: 00 is, since it seems to be the same for all timestamps (with the exception of some where it is -08: 00), but I assume this is some type of time zone offset.

I'm not quite sure which format (does anyone know?), But I need to convert it to this format:

 6/2/11 17:16:05 

which is year-month-day-time

I would like to do this in such a way that all dates in CSV (on the same line) are converted to the second format. How can I accomplish this in R?

A complete dataset can be found here .

+4
source share
2 answers

Here's another attempt, assuming your data starts with text:

 test <- c("2011-06-02T17:16:05Z","2012-06-02T17:16:05-07:00") format(as.POSIXct(test,format="%Y-%m-%dT17:%H:%M"),"%m/%d/%y %H:%M") [1] "06/02/11 16:05" "06/02/12 16:05" 
+2
source

You can try the following where myDates will be a date column

 format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%Y %H:%M") [1] "06/02/2011 16:05" "06/02/2012 16:05" or with 2-digit year # Note the lower-case %y at the end format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%y %H:%M") [1] "06/02/11 16:05" "06/02/12 16:05" 

As for Z , this indicates GMT (think London).
-7:00 indicates 7 hours ago from GMT (think: Colorado / MST, etc.)

See here for more details.

+1
source

All Articles