get system boot time in milliseconds in javascript

To get the system boot time in milliseconds using JavaScript in Node.js, you can use the systeminformation npm package along with the performance module.

Here's an example code snippet to get the system boot time in milliseconds:

index.tsx
const si = require('systeminformation');
const { performance } = require('perf_hooks');

si.time().then(data => {
  const uptimeMS = data.uptime * 1000; // convert uptime to milliseconds
  const nowMS = performance.now(); // get current time in milliseconds
  const bootTimeMS = nowMS - uptimeMS; // calculate boot time in milliseconds
  console.log(`System boot time: ${bootTimeMS.toFixed(2)} ms`);
}).catch(error => {
  console.error(error);
});
448 chars
12 lines

This code snippet uses the systeminformation.time() method to get the system uptime in seconds, which is then converted to milliseconds. The performance.now() method is used to get the current time in milliseconds, and the boot time in milliseconds is calculated by subtracting the uptime from the current time. The toFixed() method is used to format the output to two decimal places.

gistlibby LogSnag