196} How many days ago was a date? (More generally the difference between two dates)

Date tasks can also be approched using
PowerShell-aided
cmd.exe command-line.
@echo off & setlocal enableextensions
call :GetDatePS today_
echo %%today_%%=%today_%
::
set start_=05.06.2026
set endat_=%today_%
::
call :DateDiffPS "%start_%" "%endat_%" NumOfDays_
echo Start date "%start_%"
echo End date "%endat_%"
echo %%NumOfDays_%%=%NumOfDays_%%
endlocal & goto :EOF
:: The indentation is essential to avoid incorrect syntax
:GetDatePS
setlocal
for /f "usebackq delims=" %%A in (`powershell -NoLogo -NoProfile -Command ^
"Get-Date -Format 'dd.MM.yyyy'"`) do (
set "result_=%%A")
endlocal & set "%1=%result_%" & goto :EOF
:DateDiffPS
setlocal
for /f "usebackq delims=" %%a in (`
powershell -NoProfile -NoLogo -Command ^
"$s=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
"$e=[datetime]::ParseExact('%~2','dd.MM.yyyy',$null);"
"($e-$s).Days"`) do (set result_=%%a)
endlocal & set "%3=%result_%" & goto :EOF
The output might be e.g.
C:\_M>C:\_D\TEST\CMDFAQ.CMD
%today_%=21.08.2026
Start date "05.06.2026"
End date "21.08.2026"
%NumOfDays_%=77
For more PowerShell-aided cmd.exe command-line solutions see
#powershell.
The reverse
PowerShell-aided
cmd.exe command-line subroutine solution for the present item is also
of interest.
@echo off & setlocal enableextensions
set startdate_=05.06.2026
set adddate_=77
call :GetDateThenPS "%startdate_%" "%adddate_%" datethen_
echo The date %adddate_% days after %startdate_% is %datethen_%
endlocal & goto :EOF
:: The indentation is essential to avoid incorrect syntax
:GetDateThenPS
setlocal
for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
"$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
"$d.AddDays(%~2).ToString('ddd dd.MM.yyyy')"`) do (
set "result_=%%a")
endlocal & set "%3=%result_%" & goto :EOF
The output is [the weekday is given in the user account's language]
C:\_M>C:\_D\TEST\CMDFAQ.CMD
The date 77 days after 05.06.2026 is Fri 21.08.2026
As indicated, the outcome from the line
"$d.AddDays(%~2).ToString('ddd dd.MM.yyyy')"`) do (
depends on the user account's language settings. There are alternatives like
:GetDateThenPSinvariant
setlocal
for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
"$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
"$d.AddDays(%~2).ToString('ddd dd.MM.yyyy',[Globalization.CultureInfo]::InvariantCulture)"`) do (
set "result_=%%a")
endlocal & set "%3=%result_%" & goto :EOF
:GetDateThenPSus
setlocal
for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
"$d=[datetime]::ParseExact('%~1','dd.MM.yyyy',$null);"
"$d.AddDays(%~2).ToString('ddd dd.MM.yyyy',[CultureInfo]::GetCultureInfo('en-US'))"`) do (
set "result_=%%a")
endlocal & set "%3=%result_%" & goto :EOF
Instead of ('en-US') we could define e.g. ('fi-FI') or ('sw-SE') or even (sv-FI) etc.
Also see
item#006
for the earlier, less generic code for getting yesterday's and
tomorrow's date.
For other solutions (e.g. using VBS, gawk, or only original cmd script commands)
see
item#031 and
item#061