.EXAMPLE.....: 11 Find the elapsed days/hours/minutes between two date/times.
.ALIAS.......: 11
.CATEGORY....: examples
.CODE........:
This batch file was developed in cooperation with Walter Ledge, a sysop for
CompuServe's CRFORUM. In addition to being a good example of how to use
Fdate's /T parm and "#idiv" function, it should be useful for other
CompuServe sysops who need to submit the same reports that Walt does.
Here's Walt's message that started the whole thing.
As an assistant sysop on the CRFORUM, I have to submit reports to CIS on
the number of messages that are posted on our forum in terms of time per
1,000 messages -- i.e., say, I know that 1,000 messages were posted
between the hours of 17:05 on July 5 and 3:03 on July 7. I need to know
how many hours and minutes it took for those 1000 messages to be posted.
So I would like some way to use FDATE to calculate the difference
between those two times (which, of course, include the dates).
FORATIM2.BAT is an improvement of the original FORATIME.BAT batch file.
FORATIM2.BAT uses the /Fget function, which first appeared in Fdate 8.3, to
get user input from the terminal, rather than requiring all parameters to
be entered at the command line as FORATIME.BAT did.
@echo off
cls
:: ------------------------------------------------------
:: FORATIM2.BAT batch file
::
:: FUNCTION
:: Calculate the elapsed time (in days and minutes)
:: between some "begin" date/time and some "end" date/time
::
:: ------------------------------------------------------
echo --------------------------------------------------------------
echo Calculate elapsed time between two date/times
echo --------------------------------------------------------------
:BegDate
Fdate /Fget /VBEGdate /Q"Enter BEGIN DATE (mm-dd-ccyy): "
if (%BegDate%)==() goto Cleanup
:BegTime
Fdate /Fget /VBEGtime /Q"Enter BEGIN TIME (hh:mm).....: "
if (%BegTime%)==() goto Cleanup
:: validate date & time
fdate /fv /A%BEGdate% /T%BEGtime% >nul
if errorlevel 1 echo Invalid date/time
if errorlevel 1 goto BegDate
:: get absolute minute of start date/time.
fdate /ff /ominute# /A%BEGdate% /T%BEGtime% /VABStime1
if errorlevel 1 goto BegTime
fdate /ff /ofull /A%BEGdate% /T%BEGtime% /Vfull1
::
echo.
:EndDate
Fdate /Fget /VENDdate /Q"Enter END.. DATE (mm-dd-ccyy): "
if (%EndDate%)==() goto Cleanup
:EndTime
Fdate /Fget /VENDtime /Q"Enter END.. TIME (hh:mm).....: "
if (%EndTime%)==() goto Cleanup
:: validate date & time
fdate /fv /A%ENDdate% /T%ENDtime% >nul
if errorlevel 1 echo Invalid date/time
if errorlevel 1 goto EndDate
:: get absolute minute of end date/time.
fdate /ff /ominute# /A%ENDdate% /T%ENDtime% /VABStime2
if errorlevel 1 goto EndTime
fdate /ff /ofull /A%ENDdate% /T%ENDtime% /Vfull2
echo.
echo Calculating elapsed time...
:: calculate the difference between ABStime1 and ABStime2
fdate /f#dif /A%ABStime1% /B%ABStime2% /VMinutes
:: calculate the number of hours in it took
fdate /f#Idiv /A%minutes% /B60 /VHours
:: calculate the number of extra minutes it took
fdate /f#mod /A%minutes% /B60 /VMins
echo.
echo Between %full1%
echo and %full2%
echo.
echo Elapsed time was:
echo %hours% hours and %mins% minutes
fdate /f#Idiv /A%minutes% /B1440 /Vday1
fdate /f#mod /A%minutes% /B1440 /Vmin1
fdate /f#Idiv /A%min1% /B60 /Vhour1
fdate /f#mod /A%min1% /B60 /Vmin2
echo or
echo %day1% day(s) %hour1% hour(s) and %min2% minute(s).
echo.
echo.
:cleanup
set ENDdate=
set BEGdate=
set BEGtime=
set ENDtime=
set full1=
set full2=
set minutes=
set ABStime1=
set ABStime2=
set day1=
set hour1=
set min1=
set min2=
set mins=
set hours=
:endit
:13 Find the elapsed years/months/days between two dates.
.CODE........:
@echo off
:: illustrate Fdate's Month# output format
cls
echo --------------------------------------------------------------
echo YMD_DIF.BAT Calculate elapsed time between two dates
echo --------------------------------------------------------------
:: --------------------------------------------------------------
:D1
Fdate /Fget /VD1 /Q"Enter date #1 (mm-dd-ccyy), or ENTER to quit: "
if (%D1%)==() goto Cleanup
:: validate date
Fdate /Fv /A%D1% >nul
if errorlevel 1 echo Invalid date
if errorlevel 1 goto D1
:: translate date into various formats, including absolute month
Fdate /Ff /oMonth# /A%D1% /VAbsMonth1
Fdate /Ff /oday# /A%D1% /VAbsDay1
Fdate /Ff /A%D1% /VFull1
Fdate /Ff /odd /A%D1% /VDd1
:: --------------------------------------------------------------
:D2
Fdate /Fget /VD2 /Q"Enter date #2 (mm-dd-ccyy), or ENTER to quit: "
echo.
if (%D2%)==() goto Cleanup
:: validate date
Fdate /Fv /A%D2% >nul
if errorlevel 1 echo Invalid date
if errorlevel 1 goto D2
:: translate date
Fdate /Ff /oMonth# /A%D2% /VAbsMonth2
Fdate /Ff /oday# /A%D2% /VAbsDay2
Fdate /Ff /A%D2% /VFull2
Fdate /Ff /odd /A%D2% /VDd2
:: --------------------------------------------------------------
:: verify that D2 is later than D1
Fdate /Fcomp /A%D2% /B%D1% /v
if (%Fdate%)==(GT) goto EndIf1
echo D2 must be later than D1. Please try again.
echo.
goto D1
:EndIf1
:: verify that D2 is in a later month than D1
Fdate /F#comp /A%AbsMonth2% /B%Absmonth1% /v
if (%Fdate%)==(GT) goto EndIf2
:: they are in the same month
Fdate /Fdif /A%D1% /B%D2% /P"Difference is " /S" days."
echo.
goto Cleanup
:EndIf2
:: --------------------------------------------------------------
:: get difference in months
Fdate /F#dif /A%AbsMonth2% /B%Absmonth1% /vMonthsDif
:: compare days of month
Fdate /F#comp /A%Dd2% /B%Dd1% /v
if not (%Fdate%)==(LT) goto EndIf3
:: Dd2 is less than Dd1 ... subtract 1 from (add -1 to) MonthsDif
Fdate /F#add /A%MonthsDif% /B-1 /VMonthsDif
:EndIf3
:: do month arithmetic, to get a date (D3) that is
:: less than 1 month prior to D2
Fdate /Fm /A%D1% /N%MonthsDif% /Omm-dd-ccyy /VD3
:: find difference in days between D3 & D2
Fdate /Fdif /A%D3% /B%D2% /Vdaysdif
:: --------------------------------------------------------------
echo Between %Full1% & %Full2% ...
echo %MonthsDif% month(s) %DaysDif% day(s)
:: calculate the number of years, by dividing MonthsDif by 12
Fdate /F#Idiv /A%Monthsdif% /B12 /VYearsDif
:: calculate the number of extra months
Fdate /F#mod /A%MonthsDif% /B12 /VMonthsDif
echo or %YearsDif% year(s) %MonthsDif% month(s) %DaysDif% day(s)
:: --------------------------------------------------------------
:cleanup
echo.
echo.
set D1=
set D2=
set D3=
set AbsMonth1=
set AbsMonth2=
set AbsDay1=
set AbsDay2=
set Full1=
set Full2=
set Dd1=
set Dd2=
set YearsDif=
set MonthsDif=
set DaysDif=
set Fdate=
:endit