1.

Solve : Set command?

Answer»

how do u do math using the set command?
im trying to do this Set /A %TIME% - 12
the %TIME% variable is in 24 hour format so if i subtract 12 from %TIME% variable only if the the time is GREATER than 12 o'clock say its 12PM then it goes to 1PM which would be 13PM in 24 hour format. but plz help me.
I don't know where you are, but in my locale TIME has colons in it so set /a will fail

that is,

7:21:14.41

is not a valid number.

Macdad's %time% also has AM or PM appended to it. With 24-hour time setting these are superfluous.

The should work for a bare 24-hour %Time% setting:

Code: [Select]@echo off
cls

set this=%time%
set/a hour=%this:~0,2%-12
set newtime=%hour%%this:~-9%
also to overcome the 12 hour problem you have to (a) get the time in 24 hour format (b) learn about modulo arithmetic (c) decide what to do about "yesterday".

THNX Dusty it worked. Quote from: macdad- on March 20, 2008, 06:56:49 AM

thnx Dusty it worked.

GOOD, but be aware that if the hour in %Time% is less than the number of hours to be deducted you must use base 24 i.e. add 24 to the hour in %time% before deducting. e.g. if the current hour is 11 and you deduct 12 then you will get -1 whereas you really wanted 23 but 11+24-12 is 23.

Similarly when manipulating MINUTES and seconds base 60 must be used.

However, another problem(?) is that when SET/A encounters a number beginning with 0 (zero) it treats it as Octal (base eight). So if you try to do arithmetic calculations using SET/A on 08 or 09 (zeroeight or zeronine) the number is invalid in base 8 so the script will fail. My solution is to always remove the leading zero from numbers less than 10 (when dealing with two-digit numbers).

Good luck & thanks for coming back with your success story.



Discussion

No Comment Found