Reputation: 65
Hi I am running a batch script to get a current date and perform numerical operations on it.
I get the date by using the following command and then do operations on it (Add, Subtract etc).
But If the date returns a value less than 10 (eg. 09, 08) the operation gives an error.
set dd=%date:~7,2%
set /a dd1=08-1
Invalid number. Numeric constants are either decimal (17),hexadecimal (0x11), or octal (021).
Please help
Upvotes: 3
Views: 1627
Reputation: 56188
you can remove the zero with a little trick:
set /a dd1=(1%dd: =0%-100)-1
This adds the string "1" to "08" (which is also still a string), resulting in "108". Then subtract "100" (/a
treating them as numbers), which results in "8". If in your locale the date has no leading zero but a space instead, %%d: =0%
replaces it with a zero
If you need the result with leading zero, just add it again:
set dd1=0%dd1%
set dd1=%dd1:~-2%
This adds the string "0" in front of the string "7" (result from before), resulting in "07" and takes the last two digits from it "07" (in case, the result from before is "24", -> add "0" = "024" -> last two = "24")
edited to work also in locales, where the date has a space instead of a leading zero. (thanks to Lưu Vĩnh Phúc for spotting it)
Upvotes: 6