Page 1 of 1

Change the system date

Posted: 09 Jan 2020 10:23
by Geoffwl
Hi,

My windows version is 10.1.18362

I need to change the system date, run a program then change the date back again. The following works fine but requires to be run as admistrator and won't give me the right date tomorrow.



date 01-03-07
filename.exe
pause
date 05-01-20

The next loads beforedate with 05-Jan-19 and sets the date to 01-04-09. "date %BEFOREDATE%" returns "The system cannot accept the date entered. Enter the new date (dd-mm-yy). I guess it doesn't like "Jan"

set BEFOREDATE=%date:~%
date 01-04-09
filename.exe
pause
date %BEFOREDATE%


I also need to run this without administraror.

Any help greatly appreciated.

Re: Change the system date

Posted: 09 Jan 2020 17:22
by penpen
Dealing with dates is highly system dependent, so the following might (or mght not) help you (untested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
for %%a in (
	"jan=-01-" "feb=-02-" "mar=-03-" "apr=-04-" "mai=-05-" "jun=-06-"
	"jul=-07-" "aug=-08-" "sep=-09-" "oct=-10-" "nov=-11-" "dec=-12-"
) do set %%a
set "BEFOREDATE=%date:-=!%"

date 01-04-09
filename.exe
pause
date %BEFOREDATE%

goto :eof
According to the linked site, your system admin give your non-admin-user-account the needed rights to change the date:
https://www.howtogeek.com/253745/how-to ... -and-date/


penpen

Re: Change the system date

Posted: 09 Jan 2020 22:59
by Geoffwl
That works perfectly. Thank you so much for your help.

Regards

Geoffwl