Change the system date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Geoffwl
Posts: 2
Joined: 09 Jan 2020 10:12

Change the system date

#1 Post by Geoffwl » 09 Jan 2020 10:23

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Change the system date

#2 Post by penpen » 09 Jan 2020 17:22

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

Geoffwl
Posts: 2
Joined: 09 Jan 2020 10:12

Re: Change the system date

#3 Post by Geoffwl » 09 Jan 2020 22:59

That works perfectly. Thank you so much for your help.

Regards

Geoffwl

Post Reply