JSI Tip 9977. How can I calculate the difference between two short dates?


In tip 0721, I calculated the difference between two dates.

In tip 9229, I calculated the difference between two date/times?

I have scripted DateDiff.bat to calculate the difference between two short dates.

The syntax for using DateDiff.bat is:

\[call\] DateDiff ShortDate1 ShortDate2 Days

Where:

ShortDate1 First date, in your date format, like MM/DD/YYYY. Quotes are NOT allowed.

ShortDate2 Second date, in your date format, like MM/DD/YYYY. Quotes are NOT allowed.


Days       is a call directed numeric environment variable that will contain the difference:
           Days = ShortDate1 - ShortDate2. 
           Days may be negative.

Example

To calculate the number of days till Christmas, call XMAS 12/25/2006 Days, where XMAS.BAT contains:
@echo off
if \{%2\}

\{\} @echo Syntax: XMAS DateOfXmas Diff&goto :EOF setlocal call DatePorM 0 Today call DateDiff %1 %Today% Diff @echo %Diff% endlocal&set /a %2=%Diff%

DateDiff.bat contains:
@echo off
if \{%3\}\{\} @echo Syntax: DateDiff ShortDate1 ShortDate2 Days&goto :EOF
if exist "%TEMP%\DateDiff.vbs" goto doit
@echo Dim dDate1, dDate2, dDiff>"%TEMP%\DateDiff.vbs"
@echo dDate1 = CDate(WScript.Arguments(0))>>"%TEMP%\DateDiff.vbs"
@echo dDate2 = CDate(WScript.Arguments(1))>>"%TEMP%\DateDiff.vbs"
@echo dDiff = dDate1 - dDate2>>"%TEMP%\DateDiff.vbs"
@echo WScript.Echo dDiff>>"%TEMP%\DateDiff.vbs"
:doit
for /f "Tokens=*" %%d in ('cscript //nologo "%TEMP%\DateDiff.vbs" %1 %2') do (
 set %3=%%d
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish