I have a local time like \"2013-08-27 10:01:22\"
, how do I convert it to epoch time?
basically I need the opposite of as.POSIXct
, I searched on
The accepted answer truncates time to the whole second. POSIXct
actually provides sub-second resolution, though. As mentioned in the comments by “statquant”, you can use as.numeric
to obtain the exact epoch:
result = as.numeric(as.POSIXct(Sys.time()))
Beware that with the default options for digit display in R this will look like it has no digits behind the decimal point:
> result
[1] 1480599768
However, these are simply truncated in the display. To make them visible, use:
> dput(result)
1480599767.58447
… or set options('digits')
to a higher value.