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 . Does anyone have any script for that? Or is it too difficult to do it in bat file?
Let me know.
Thanks,
Meet
Addition of days
Moderator: DosItHelp
Re: Addition of days
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
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
hybrid CMD/VBS date calculation: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.
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
-
- Posts: 15
- Joined: 14 Jun 2010 06:50
Re: Addition of days
@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?
Just calling dayadd (function ??) and adding/subtracting 2 from the date, rite?
I would like to know what exactly is going on here. What's WScript? Is DateAdd function inbuilt into it?
Thanks in advance,
Meet
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
yesagrawal.meet wrote:Just calling dayadd (function ??) and adding/subtracting 2 from the date, rite?
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:agrawal.meet wrote:I would like to know what exactly is going on here. What's WScript? Is DateAdd function inbuilt into it?
Code: Select all
WScript.Echo DateAdd("D",-2,Now)
DateAdd() - VBS function for date manipulation
-
- Posts: 15
- Joined: 14 Jun 2010 06:50
Re: Addition of days
Thanks a lot amel27. That was really helpful.
Thanks,
Meet
Thanks,
Meet