Is there any way to get current time in nanoseconds using JavaScript?

前端 未结 7 937
名媛妹妹
名媛妹妹 2020-11-30 12:11

So, I know I can get current time in milliseconds using JavaScript. But, is it possible to get the current time in nanoseconds instead?

7条回答
  •  一生所求
    2020-11-30 12:36

    In Server side environments like Node.js you can use the following function to get time in nanosecond

    function getNanoSecTime() {
      var hrTime = process.hrtime();
      return hrTime[0] * 1000000000 + hrTime[1];
    }

    Also get micro seconds in a similar way as well:

    function getMicSecTime() {
      var hrTime = process.hrtime();
      return hrTime[0] * 1000000 + parseInt(hrTime[1] / 1000);
    }

提交回复
热议问题