get system boot time in seconds in typescript

To get the system boot time in seconds, we can use the sysinfo package in Node.js. Here's an example in TypeScript:

index.ts
import * as os from 'os';
import * as si from 'systeminformation';

async function getBootTimeInSeconds(): Promise<number> {
  const bootTimeInSeconds = await (async () => {
    const sysInfo = await si.get({ system: false });
    return (sysInfo.btime * 1000 - os.uptime() * 1000) / 1000;
  })();
  return bootTimeInSeconds;
}

// usage
getBootTimeInSeconds().then((bootTimeInSeconds) => {
  console.log(`System boot time: ${bootTimeInSeconds} seconds`);
});
460 chars
16 lines

Here, we first import the os and systeminformation modules. Then, we define an async function to get the boot time in seconds.

Within the function, we first use the systeminformation package to get the boot time of the system in milliseconds. We subtract the current uptime in milliseconds from this value to get the system boot time in milliseconds. We then divide this value by 1000 to get the boot time in seconds.

Finally, we call the function and log the result to the console.

gistlibby LogSnag