您可以使用以下代码来获取系统正常运行时间,并将其转换为天:小时:分钟:秒的时间格式:

const si = require('systeminformation');

si.time().then(data => {
  const uptime = data.uptime;
  const days = Math.floor(uptime / (24 * 60 * 60));
  const hours = Math.floor((uptime % (24 * 60 * 60)) / (60 * 60));
  const minutes = Math.floor((uptime % (60 * 60)) / 60);
  const seconds = Math.floor(uptime % 60);

  const formattedTime = `${days}:${hours}:${minutes}:${seconds}`;
  console.log(formattedTime); // 输出格式化后的时间
}).catch(error => {
  console.error(error);
});

这段代码使用'systeminformation'模块中的'time()'方法来获取系统的正常运行时间。然后,通过将秒数转换为天、小时、分钟和秒来格式化时间。最后,将格式化后的时间打印到控制台。

请确保在运行代码之前已经安装了'systeminformation'模块,可以使用以下命令进行安装:

npm install systeminformation
Node.js 使用 systeminformation 获取系统运行时间并格式化

原文地址: https://www.cveoy.top/t/topic/R7C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录