Set and get the windows system time


#1

How can I set and get the windows system time from an electron application?
Is it possible?


#2

I don’t believe there’s anything in Node to do that directly, but you could probably do it in a C or C# binary and have your application call the binary.


#3

A simple bat file can solve this problem:
time 12:00:00

But how can I run this bat file with administrative privileges?


#4

Super User has answers.


#5

Based on this answer http://superuser.com/a/852877

test.bat file:

@echo off
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %*", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

time %*

And electron can execute it:
require("child_process").exec('test.bat 15:00:00').unref();

Thanks the help