How to Set Clock Back 5 minutes in Dos?

September 13, 2009 at 06:00:12
Specs: Windows XP
How can you set th time back 5 minutes in a batch file?

Do you have to grab the time first like
set x = %time%

then subtract it....
x = x-5

set time = x

that doesn't seem to work...DOS is confusing me....

See More: How to Set Clock Back 5 minutes in Dos?

Report •

September 13, 2009 at 06:07:26
Doing time math [or date math] is difficult at best in XP.

And essentially impossible in DOS.

Depending on your installation of DOS, if indeed you have one, you may have BASIC. Which has a better shot at time math.

There are probably millions of lines of BASIC out there for the finding.

Helping others achieve escape felicity


Report •

September 13, 2009 at 11:23:28
In lack of something better you could try a expedient like this (I suppose your need is not to professional ends).

@echo off
setlocal enabledelayedexpansion
set date0=%date%
::set time0=%time:~0,8%
set /a ctt=0
set t1=%time:~6,2%
set t2=%time:~6,2%
if not %t2%==%t1% (
set t1=%t2%
set /a ctt+=1
if %ctt%==300 goto :next
goto :time

Hear we need a help.
I tried to set time variable but not succeeded. (set time=%time0%
And: if not current date==%date0% set date=%date0%

Limitdos, this bat will run for 5 minutes. If you read a couple of posts of this section (batch) very soon you will be doing something better by yourself. This is a fantastic place to learn, the better I know.

Report •

September 13, 2009 at 12:19:19
I think this may work for you, I think I've covered the problems you would run into with the if statements. The only problem you may run into is if you set the clock back past midnight it won't change the date. But I think every other case you may run into has been covered.

@echo off
set /a mm=%time:~3,2%
set /a hh=%time:~0,2%
if %mm% lss 5 set /a hh=%hh%-1
if %mm% lss 5 set /a mm=%mm%+60
set /a mm=%mm%-5
if %mm% lss 10 set mm=0%mm%
if %hh% equ -1 set hh=23
time %hh%:%mm%%time:~5,6%
set mm=
set hh=

Report •

Related Solutions

September 13, 2009 at 15:49:12
I think the OP wants to reset the system time in which case the SetX command will probably have to be used somewhere along the line.

Report •

September 13, 2009 at 17:26:35
To set the time in dos you simply use the "time" command with the time in the 24 hour format, so to set 7:06PM you would type "Time 19:06". Unfortunately I'm not aware of any commands that would let you simply subtract 5 minutes. So the only way I can think that is to set the Minutes as a variable, "set mm=%time:~3,2%" which is saying basically create a variable called MM which is equal to the minutes of the time varible %time:~3,2% is saying in the time variable skip to the 3rd character (dos starts counting at 0) and than show 2 characters. Then from there you can subtract 5 from the number but you have to add some "if statements" to correct the formatting, for instance if the minute was "4" if you subtract 5 you would get -1 instead of "59" and then if you understand that, the rest is self explanatory logic to change the hour and day depending on when you run the batch.

In response three I am using the "time" command to set the time, all of the "set" commands are simply formatting the minute number to work with the Time command and also removing the 5 minutes. Which I just played with a little and found I might have over killed it a little bit. The following will do the samething, the time stuff has been streamlined as much as I could, and then the logic to change the computers date back if the 5 minutes rolls over midnight has been added.

@echo off
set /a mm=%time:~3,2%
set /a hh=%time:~0,2%
set /a dd=%date:~7,2%
if %mm% lss 5 (set /a hh=%hh%-1) & set /a mm=%mm%+60
if %hh% equ -1 (set /a hh=23) & set /a dd=%dd%-1
set /a mm=%mm%-5
time %hh%:%mm%
date %date:~4,2%-%dd%-%date:~-4%
(set mm=) & (set hh=) & set dd=

Report •

September 13, 2009 at 18:05:42

Using setx on %time% is a bad idea, at best it will fail at worst override it's dynamic nature. Time is indeed the correct command to use.


You must be careful using the date math as the output cannot be guaranteed.

If your using XP try this out for fun


Category view: date, time, language, and regional options> change the format of numbers, dates and times.>
Classic view: Regional and language options>

customize > date

In the short date field type in "ddd ddd" (without quotes) apply the settings and open up a command prompt and "echo %date%".

Of course this is an unlikely situation, but it's not unlikely to have any of a few common formats, making substrings fail.

I forgot that there is also the issue of the day rolling back the month or year.

Here is a script I wrote a while back to get yesterdays date on xp with a good probability of success, as far as I know it will only work on xp, it returns an errorlevel.

Report •

September 13, 2009 at 18:23:42
Good call, also I didn't take into account the fact that on the occasion that this was ran just passed midnight on the first of a month that it would try to set the date as "0" plus you have a very valid point Judago, So here again is the code to remove 5 minutes from the system clock, with out the ability to roll back days.

@echo off
set /a mm=%time:~3,2%
set /a hh=%time:~0,2%
if %mm% lss 5 (set /a hh=%hh%-1) & set /a mm=%mm%+60
if %hh% equ -1 set /a hh=23
set /a mm=(%mm%-5)
time %hh%:%mm%
(set mm=) & set hh=

Report •

September 13, 2009 at 18:48:44
guys...thank you so much for all the help....

so, I'm a little new to dos....but here:
set /a mm=%time:~3,2%
set /a hh=%time:~0,2%

you're basically setting the current minutes and hours as variables mm and hh

and then here:
time %hh%:%mm%
you're setting time equal to mm and hh?
I'm always a little confuse by the percent signs....they represent variables or something?

so %hh% is the same as variable hh?

also, what is the last line for:
(set mm=) & set hh=
it looks like you are not setting hh to anything in the last line....

thanks again...

Report •

September 13, 2009 at 18:50:21
I think using a bat file or program is dangerous. Other programs (i.e. Timesheets and accounting or more dangerous process control) may depend upon the correct system time. As a side note in the period just after dare change without date adjustment you will add 23 hrs 55 min.

Report •

September 13, 2009 at 20:06:35
Yeah, you use the "set" command (help file available in command prompt by typing "set /?") to make variables called MM and HH and setting them to there respective part of the time variable.

The % signs simply expands the variable (shows what is stored in the variable) Think of the variable as a container, it stores some information for you. To get that information back out of it, you need to expand or open the variable using the % symbol.

So %hh% would actually show you what is stored in the variable named hh. I hope that makes some sense.

So if hh was set to 5
and mm was set to 19
then the computer is going to actually see "time %hh%:%mm%" as "time 5:19"

Then at the vary end I just used the set command again to set hh and mm as nothing. It just looks funny becuase I used the "&" symbol to put both command on one line.

Report •

September 13, 2009 at 21:05:37

Report •

September 13, 2009 at 21:34:56
He he...

Perhaps he wants to get out of school/go to lunch early.

Clocks probably syncing to a local server...

Those policies will also effect the command line anyway, I have the same issue at work ;). It can be changed in the bios but on boot up it will change right back.

Report •

September 14, 2009 at 03:18:09
Thanks for the heads up. So if the OP has no access to the system clock on the command line, he won't have access in a batch file either.

Still, I was thinking of a way to solve this (as a brain exercise). Since there is no guarantee that a batch file can do time/date arithmetic correctly, there is only one way...

set savetime=%time%
echo Please go and have a cup of coffee.
ping -n 300 -w 1000>nul
time %savetime%
echo Clock has been set back 5 minutes. Hope you enjoyed your coffee.

[Edit: after posting, I've just realised this still doesn't deal with date changes.]

Report •

Ask Question