Page 1 of 1

Addition of days

Posted: 14 Jun 2010 07:14
by agrawal.meet
Hello All,

Hope everyone is doing well. Had a question so joined the forum. Here goes:

I am currently appending date and time to a file name. I recently got a requirement to add/subtract 2 days and then append it.

Questions:
1. Do we have any inbuilt date function in DOS?
2. I tried to split the date portion and add it with 2 but can't seem to find a solution to change the month/year if date happens to be 29 December :x . Does anyone have any script for that? Or is it too difficult to do it in bat file? :)

Let me know.

Thanks,
Meet

Re: Addition of days

Posted: 14 Jun 2010 15:43
by aGerman
Well, batch doesn't know any calculation with DateTime values (BTW normal calculations are difficult enough, think about floating point values, batch only knows 32Bit integer values...).
Afaik there is only one way to calculate DateTime values - conversion to a number of days or seconds.

For days you could use the DosTips functions date2jdate and jdate2date.

I'd like to refer to a site of Ritchie Lawrence too.
http://www.commandline.co.uk/lib/treeview/index.php
Have a look to the batch functions "DateToSecs" and "SecsToDate".

But I would prefer a small included VBScript for easy calculation of DateTime values...

Regards
aGerman

Re: Addition of days

Posted: 14 Jun 2010 18:30
by amel27
agrawal.meet wrote:I am currently appending date and time to a file name. I recently got a requirement to add/subtract 2 days and then append it.
hybrid CMD/VBS date calculation:

Code: Select all

@echo off

call :dayadd +2
echo [now+2] %$d% %$t%& pause >nul

call :dayadd -2
echo [now-2] %$d% %$t%& pause >nul

:dayadd
echo WScript.Echo DateAdd("D",%~1, Now) >"%tmp%\%~n0.vbs"
for /f "tokens=1*" %%i In ('cscript //nologo "%tmp%\%~n0.vbs"') do set $d=%%i& set $t=%%j
goto :eof

Re: Addition of days

Posted: 15 Jun 2010 02:04
by agrawal.meet
@aGerman
Thanks a lot for the reply. I will visit the site and see.

@amel27
Thanks!!! That worked. I am still a rookie into batch programming. Can you please tell me what exactly are we doing here?

call :dayadd +2
echo [now+2] %$d% %$t%& pause >nul

call :dayadd -2
echo [now-2] %$d% %$t%& pause >nul

Just calling dayadd (function ??) and adding/subtracting 2 from the date, rite?

:dayadd
echo WScript.Echo DateAdd("D",%~1, Now) >"%tmp%\%~n0.vbs"
for /f "tokens=1*" %%i In ('cscript //nologo "%tmp%\%~n0.vbs"') do set $d=%%i& set $t=%%j
goto :eof


I would like to know what exactly is going on here. What's WScript? Is DateAdd function inbuilt into it?


Thanks in advance,
Meet

Re: Addition of days

Posted: 15 Jun 2010 02:37
by amel27
agrawal.meet wrote:Just calling dayadd (function ??) and adding/subtracting 2 from the date, rite?
yes
agrawal.meet wrote:I would like to know what exactly is going on here. What's WScript? Is DateAdd function inbuilt into it?
batch create VBS-script in TEMP directory (with same name as original), wich adding/subtracting days from current date and return result to CMD script, VBS content sample:

Code: Select all

WScript.Echo DateAdd("D",-2,Now)
"WScript.Echo" - VBS analog of CMD "Echo"
DateAdd() - VBS function for date manipulation

Re: Addition of days

Posted: 15 Jun 2010 05:27
by agrawal.meet
Thanks a lot amel27. That was really helpful.

Thanks,
Meet