Printer Version Table of Contents Project Home Page
.EXAMPLE.....: 13 Find the elapsed years/months/days between two dates.
.CATEGORY....: examples
.DISCUSSION..:
.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