I'm writing this .bat that calculates the past day.
title Past Day
set /a dayNum=%date:~0,2% - 1
set monthNum=%date:~3,2%
set yearNum=%date:~6,4%
rem Checks if it's first day of the month
if %dayNum%==0 (
set /a monthNum-=1
rem Checks the last day of past month
rem For 0, 1, 3, 5, 7, 8, 10 => 31, where 0 is December[12]
rem For 4, 6, 9, 11 => 30
rem For 2 => 28 or 29[leap]
for %%a in (0, 1, 3, 5, 7, 8, 10) do (
if %monthNum%==%%a (
rem If, December, yearNum-1 and monthNum = 12
if %monthNum%==0 (
set /a monthNum=12 * 1
set /a yearNum-= 1
)
set /a dayNum=31 * 1
)
)
for %%a in (4, 6, 9, 11) do (
if %monthNum%==%%a (
set /a dayNum=30 * 1
)
)
if %monthNum%==2 (
rem Leap year check
set /a leap=!(%yearNum% %% 4) - !(%yearNum% %% 100) + !(%yearNum% %% 400)
if %leap%==1 (
set /a dayNum=29 * 1
) else (
set /a dayNum=28 * 1
)
)
)
rem Adds leading zero
if 0%dayNum:~1,1%==0 (
set dayNum=0%dayNum%
)
If it's not first day, it works well, but if I force dayNum to be 0 (indicating it is first day), the output is:
>title Past Day
>set /a dayNum=0
>set monthNum=03
>set yearNum=2020
>rem Checks if it's first day of the month
- was unexpected at this moment.
> set /a leap=!(2020 % 4) - !(2020 % 100) + !(2020 % 400)
Using echo, the variables contents are the same as they started and, I think, the code didn't entered the if
statement.
The expected output is to get like dayNum=31
or dayNum=29
(if leap year) and so on.