如何在Node.js中获得microtime? [英] How to get a microtime in Node.js?

查看:140
本文介绍了如何在Node.js中获得microtime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Node.js中获得最准确的时间戳?

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

ps我的Node.js版本是0.8.X和 node-microtime扩展对我不起作用(安装时崩溃)

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

推荐答案

new Date()。getTime()?这给你一个以毫秒为单位的时间戳,这是JS给你的最准确的。

new Date().getTime()? This gives you a timestamp in milliseconds, which is the most accurate that JS will give you.

更新:如vaughan所述, process.hrtime ()在Node.js中可用 - 它的分辨率是纳秒,因此它的分辨率要高得多,这并不意味着它必须更精确。

Update: As stated by vaughan, process.hrtime() is available within Node.js - its resolution are nanoseconds and therefore its much higher, also this doesn't mean it has to be more exact.

PS:为了更清楚, process.hrtime()返回一个元组数组包含当前高分辨率实时,[秒,纳秒]

PS.: Just to be clearer, process.hrtime() returns you a tuple Array containing the current high-resolution real time in a [seconds, nanoseconds]

这篇关于如何在Node.js中获得microtime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆