Search code examples
rstrptime

error in getting the correct date using strptime in R


I'm using strptime to extract date and the result is a wrong year Where is the error in the below code:

strptime('8/29/2013 14:13', "%m/%d/%y") 
[1] "2020-08-29 PDT"

What are the other ways to extract date and time as separate columns. The data I have is in this format - 8/29/2013 14:13 I want to split this into two columns, one is 8/29/2013 and the other is 14:13.


Solution

  • You have a four digit year so you need to use %Y

    strptime('8/29/2013 14:13', "%m/%d/%Y" )
    [1] "2013-08-29 CEST"
    

    Do you really want data and time in separate columns? It usually much easier to deal with a single date-time object.