Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am trying to figure out how to do arithmatic with dates.
If I want to find out what the date will be 2 days from now, I've tried
set /a enable_date=%date% + 3
it treats it as a string and echoing the variable just displays "14/11/2005+3"I have tried subracting dates, so for example
set /a enable_date=20/11/2005 - %date%
but echong the variable in that situation just gives "0" (its the 14/11/2005 today so it should really return 6 if it was working)Anyone have any ideas on how I can do this?
I've considered splitting the date up into DD MM YYYY and then adding a number to the DD and putting it all back together but that seems a bit complicated as I'd then have to take into account incrementing the month if the day reaches the end of that particular month, and then the year if its the end of december.
Thanks for any help

Matthew, you just have answered by yourself: date math is cumbersome and absolutely no friendly to perform using strictly the batch language. There is no builtin functions to handle that issue, so better you try another scripting tool (e.g. Perl), code your own utility in the language you like or look for some tool on the net.

Hi Matthew & IVO,
Yeah, as Dr. McCoy always tell Captain Kirk, it's worse than that, Jim.
Seems dtech10 has worked the batch issue:
http://computing.net/programming/wwwboard/forum/13207.html
I'm working on a solution in C.
If at first you don't succeed, you're about average.M2

Hi
This help I'm assuming an English Date.
Usage Prog.bat (Number Of Days)
ie Prog 14 ;Date in 14 days Time
ie Prog -12 ;Date 12 Days Ago
Is this what you require or the number of days between dates.------------
@echo off
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (
set dd=%%a
set mm=%%b
set yy=%%c
)
set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084
echo Today=%dd%/%mm%/%yy%if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%set /a jd=%jd%+%1
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
set NDate=%dd%/%mm%/%yy%
echo NDate=%NDate%
for %%a in (a b dd mm yy jd mo yr) do set %%a=

Hi Matthew & IVO & dtech10,
Take a look at my new post in programming.
Not exactly math, but a small step in finding 'older' files.
If at first you don't succeed, you're about average.M2

Thanks for the help,
dtech10, thats what I needed cheers. I'll need to make a small change though as the date will not always be the current date, so need to be able to be able to type in another date at the prompt. Shouldn't be too hard to that I think.
Thanks again everyone.

This Help
Usage: Prog.bat 15/11/2005 4
Add 4 Days to 15th Nov 2005
@echo off
echo %1 > ~Date.txt
for /f "tokens=1-4 delims=/ " %%a in (~Date.txt) do (
set dd=%%a
set mm=%%b
set yy=%%c
)
set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084
echo Today=%dd%/%mm%/%yy%if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%set /a jd=%jd%+%2
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
set NDate=%dd%/%mm%/%yy%
echo NDate=%NDate%
for %%a in (a b dd mm yy jd mo yr) do set %%a=
del ~Date.txt

Thanks dtech10,
just a note though, I noticed yesterday and today, the script does not appear to work too well with days less than 10 (so 1st to 9th), so todays date for example (09/12/2005) ended up giving the error:
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).its this line which caused the problem once it gets to the adding the %dd%:
jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084I've added the following code before that arithmatic line and it works again:
if %dd:~0,1% EQU 0 (
set dd=%dd:~1,1%
)Could you please explain to me what the various numbers are actually for? Just would like to know what they are referring to or how you came to those values
jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084thanks again

Hi Matthew,
I won't butt into dtech10's cade.
Are you asking about the "octal gotcha"?
For some reason, M$ decided that a number starting with a 0 is octal.
Your stripping of the leading zero will work.
Also this slick trick from another guy's code:
set /a x=108-100
or
set /a x=1%dd%-100
If at first you don't succeed, you're about average.M2Go

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |