Search code examples
rdatetimeunix-timestamp

How to convert to unix time in r?


I want to convert this date format to unix timestamp.I need this to find difference between two columns of dates.

Fri Apr 01 04:32:50 +0000 2011

What is the better way to do it in R? Should I remove first symbols that represent weekday and than use as.POSIXlt()?


Solution

  • Just use strptime and cast to numeric:

    ts <- strptime("Fri Apr 01 04:32:50 +0000 2011",  "%a %b %d %H:%M:%S %z %Y")
    as.numeric(ts)
    
    [1] 1301632370
    

    Demo