Addition of days

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

Addition of days

#1 Post by agrawal.meet » 14 Jun 2010 07:14

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Addition of days

#2 Post by aGerman » 14 Jun 2010 15:43

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

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Addition of days

#3 Post by amel27 » 14 Jun 2010 18:30

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

agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

Re: Addition of days

#4 Post by agrawal.meet » 15 Jun 2010 02:04

@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

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Addition of days

#5 Post by amel27 » 15 Jun 2010 02:37

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

agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

Re: Addition of days

#6 Post by agrawal.meet » 15 Jun 2010 05:27

Thanks a lot amel27. That was really helpful.

Thanks,
Meet

Post Reply