How to generate timestamp unix epoch format nodejs?

后端 未结 7 1656
感动是毒
感动是毒 2021-01-31 06:54

I am trying to send data to graphite carbon-cache process on port 2003 using

Ubuntu terminal:

echo "test.average 4 `date +%s`" | nc -q0 127.0.0.1         


        
相关标签:
7条回答
  • 2021-01-31 07:47

    The native JavaScript Date system works in milliseconds as opposed to seconds, but otherwise, it is the same "epoch time" as in UNIX.

    You can round down the fractions of a second and get the UNIX epoch by doing:

    Math.floor(+new Date() / 1000)
    

    Update: As Guillermo points out, an alternate syntax may be more readable:

    Math.floor(new Date().getTime() / 1000)
    

    The + in the first example is a JavaScript quirk that forces evaluation as a number, which has the same effect of converting to milliseconds. The second version does this explicitly.

    0 讨论(0)
提交回复
热议问题