How to get a microtime in Node.js?

后端 未结 13 1176
情深已故
情深已故 2020-12-04 13:41

How can I get the most accurate time stamp in Node.js?

ps My version of Node.js is 0.8.X and the node-microtime extension doesn\'t work for me (crash on install)

13条回答
  •  攒了一身酷
    2020-12-04 14:25

    process.hrtime() not give current ts.

    This should work.

     const loadNs       = process.hrtime(),
            loadMs       = new Date().getTime(),
            diffNs       = process.hrtime(loadNs),
            microSeconds = (loadMs * 1e6) + (diffNs[0] * 1e9) + diffNs[1]
    
      console.log(microSeconds / 1e3)
    

提交回复
热议问题