In Windows command-line I can print the year of the current date by doing this:

echo %date:~10,4%

result: 2016

Of course, depending on how dates are displayed in your language you need to change the 10 or 4 to get the correct sub string. But that's ok.

What I want it to print is 2015, which is 2016 - 1. So, how do I subtract 1 from the sub string I've just captured? There is no problem in using auxiliary variables.

  • this date stuff is way to complex with the windows batch shell and also kind of unreadeable. I'd suggest using PowerShell. (Get-Date).year -1 simple and clear. gotta love powershell. put it into a variable for further use. – SimonS Feb 12 '16 at 18:24

As you've stated there's no problem using another variable, you can do it as follows (My environment settings use a different substring)

set /a "yearminus1=%date:~6,10%-1"

enter image description here

For you, I assume this will be set /a "yearminus1=%date:~10,4%-1", but I can't test this.

Using the /a switch with set specifies you will be giving an expression to be evaluated, rather than just store the expression in the variable as it is.

The following operators can be used:

   +   Add                set /a "_num=_num+5"
   +=  Add variable       set /a "_num+=5"
   -   Subtract (or unary)set /a "_num=_num-5"
   -=  Subtract variable  set /a "_num-=5"
   *   Multiply           set /a "_num=_num*5"
   *=  Multiply variable  set /a "_num*=5"
   /   Divide             set /a "_num=_num/5"
   /=  Divide variable    set /a "_num/=5"
   %   Modulus            set /a "_num=5%%2"
   %%= Modulus            set /a "_num%%=5" 
   !   Logical negation  0 (FALSE) ⇨ 1 (TRUE) and any non-zero value (TRUE) ⇨ 0 (FALSE)
   ~   One's complement (bitwise negation) 
   &   AND                set /a "_num=5&3"    0101 AND 0011 = 0001 (decimal 1)
   &=  AND variable       set /a "_num&=3"
   |   OR                 set /a "_num=5|3"    0101 OR 0011 = 0111 (decimal 7)
   |=  OR variable        set /a "_num|=3"
   ^   XOR                set /a "_num=5^3"    0101 XOR 0011 = 0110 (decimal 6)
   ^=  XOR variable       set /a "_num=^3"
   <<  Left Shift.    (sign bit ⇨ 0)
   >>  Right Shift.   (Fills in the sign bit such that a negative number always remains negative.)
                       Neither ShiftRight nor ShiftLeft will detect overflow.
   <<= Left Shift variable     set /a "_num<<=2"
   >>= Right Shift variable    set /a "_num>>=2"

  ( )  Parenthesis group expressions  set /a "_num=(2+3)*5"
   ,   Commas separate expressions    set /a "_num=2,_result=_num*5"

From here.

  • set /a "yearminus1=%date:~6,10%-1" gives me "Missing operand" – johny why Sep 21 '16 at 17:43

How do I set a variable that is the year of last year?

Use the following batch file (test.cmd):

@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=/ tokens=3 " %%i in (`date /t`) do (
  echo Current year is %%i
  set /a "_lastyear=%%i-1"
  echo Last year is !_lastyear!


Current year is 2016
Last year is 2015

In your particular case you should be able to use the following:

set /a "_lastyear=%%i-1"
echo %_lastyear%

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • date - Display or change the date.Display or change the date.
  • for /f - Loop command against the results of another command.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • Actually I can. The problem in your case was that your date should be something like 2016/02/12 and by using 10 as an index parameter you couldn't extract the substring because it's too short (indexes ranging from 0 to 9). Try echo %date% or echo %date:~0,4% and it's going to work. – Gustavo Feb 12 '16 at 16:07
  • Regardless, my answer shows how to "subtract 1 from the sub string I've just captured?" – DavidPostill Feb 12 '16 at 18:29
  • I added a few lines to the answer to address your specific case. – DavidPostill Feb 12 '16 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.