How to convert a formatted local time to epoch?

后端 未结 3 936
渐次进展
渐次进展 2021-02-07 07:17

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

3条回答
  •  旧时难觅i
    2021-02-07 08:05

    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.

提交回复
热议问题