I have written a rather small batch file that does some date calculations for me.
However, I have stumbled upon a minor problem:
Whenever I try to increment my month var, it simply sets the var to the desired incrementation. Code:
: How much time should we add? Default is 1 month
SET timeadd=1
: Set the date variables to build needed format
SET YYYY=%date:~-4%
SET MM=%date:~3,2%
SET DD=%date:~0,2%
if not %MM%==12 (
SET /A MM=MM+timeadd
SET changed=1
)
After the declaration, the month var would today (August 17th) be “08”.
After the if block however, this var would change to “1”.
I could guess that this is a conversion problem (date beeing a string, timeadd beeing an integer), however I really have no clue how to solve this.
As said in the comments, this is happening because a leading
0indicates octal notation (just like a leading0xindicates Hex notation), but the octal numbering system runs from 01 to 07, so there is no octal 08 or 09, rather it would be 010 and 011.Decimal Value 8 == Octal Value 010
Decimal Value 9 == Octal Value 011
To verify this for yourself, type this at the command-prompt
Your output will be:
=======================================
My solution to this is simply to check for any leading
0‘s and delete them.Do any mathmatical magic I need done…
…and then add the leading
0‘s back on if necessary.Just remember to make sure that it doesn’t exceed 12 (in this instance) or get lower than 1.