1.

Solve : determine if today's date is odd or even?

Answer»

Hi folks, I've got this bit of code which is causing me a lot of puzzlement.

Code: [Select]REM Parse date into YYYYMMDD
SET YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

REM See if date is even or odd
SET /a odd=%YYYYMMDD%%2

When I type that into the command line, %YYYYMMDD% comes out to 20080109 and %odd% ends up as 1 (well, it does today anyway). However when I run this from a batch file, %odd% is set to 20080109. Can anyone shed some light on this?

I'm running windows XP, service pack 2.

Thanks very much!you have set YYYYMMDD wrong ,

try set yyyymmdd=%date:~6,9%%date:~3,2%%date:~0,2%mmm... I don't think so, either way I get "20080109" for yyyymmdd which is what I want, so I don't think it's that part that's causing the problem, but the odd/even part. Your suggestion gives me "/09/2008 0We" which isn't right either.% is a special CHARACTER when it comes to batch code. Try doubling up for the modulo result:

Code: [Select]@echo off
REM Parse date into YYYYMMDD
SET YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

REM See if date is even or odd
SET /a odd=%YYYYMMDD%%%2
if %odd%==0 echo Today %date% is even
if %odd%==1 echo Today %date% is odd
ah, now that doesn't work from the command line but it does in a batch file. Thanks very much.

Out of curiosity, and offtopic a bit, why the difference?QUOTE

% is a special character when it comes to batch code



Discussion

No Comment Found