[英]Node.js - Set system date / time
Is there a way to set the date/time on the operating system from a node.js server? 是否可以通过node.js服务器在操作系统上设置日期/时间?
There are plenty of examples on how to change the time-zone but i need to change the actual date / time of the PC 关于如何更改时区,有很多示例,但是我需要更改PC的实际日期/时间。
My answer is based of of @Mimouni's answer https://stackoverflow.com/a/23156354/1799272 on a different question, I just changed the include of sys to util, because of deprecation. 我的答案基于@Mimouni的答案https://stackoverflow.com/a/23156354/1799272 ,它基于另一个问题,由于弃用,我刚刚将sys的include更改为util。
Furthermore due to our implementation and requirements (we start other windows services if the time was wrong) I imported 'node-windows'. 此外,由于我们的实施和要求(如果时间不正确,我们将启动其他Windows服务),我导入了“ node-windows”。 However you are welcome to see if you can elevate the user without this if you only want to use built in npm features.
但是,如果您只想使用内置的npm功能,欢迎您查看是否可以在不使用此功能的情况下提升用户。
basically you have 3 important steps: 基本上,您有3个重要步骤:
Here as follows: 如下:
const sys = require('util')
const win = require('node-windows')
dateTime = new Date(time) //Convert string or number to date
let day = dateTime.getDate()
let month = dateTime.getUTCMonth() + 1
let year = dateTime.getFullYear()
let updateD = `${year}-${month}-${day}` //Format the string correctly
//Add a callback function (this can be somewhere else)
function execCallback(error, stdout, stderr) {
if (error) {
console.log(error)
} else {
console.log(stdout)
}
}
var exec = win.elevate(`cmd /c date ${updateD}`,undefined, execCallback);
if you wish to set the time, replace date in the exec with time . 如果你想设置的时间,在时间的高管更换日期 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.