核心逻辑是调用 node-windows 包,执行一个 cmd 命令...


const win        = require("node-windows");

/**
 *  设置系统时间
 * */
const setSystemTime = function (dateTime){

  return new Promise((resolve, reject) => {

    let year       = dateTime.getFullYear()
    let month      = dateTime.getUTCMonth() + 1
    let day        = dateTime.getDate()
    let h          = dateTime.getHours() < 10 ? '0' + dateTime.getHours() : dateTime.getHours()
    let mm         = dateTime.getMinutes() < 10 ? '0' + dateTime.getMinutes() : dateTime.getMinutes()
    let s          = dateTime.getSeconds() < 10 ? '0' + dateTime.getSeconds() : dateTime.getSeconds()
    let updateDate = `${year}-${month}-${day} ${h}:${mm}:${s}`
    let updateTime = `${h}:${mm}:${s}`
    //win.elevate(`cmd /c date ${updateDate}`);
    win.elevate(`cmd /c time ${updateTime}`, undefined, function (error, stdout, stderr){

      if (error) {
        throw new Error(error);
      } else {
        resolve(stdout);
      }
    })
  });


}

标签: none