代码

# Usage: call :datecounter <Date 1> <Date 2>
# Example: call :datecounter 2021-3-5 2024-8-3

call :datecounter 2020-9-1 2022-11-29
echo %datecount%
pause&exit

:datecounter
for /f "delims=" %%a in ('powershell -NoProfile -c "[Math]::Abs(((Get-Date "%1")-(Get-Date "%2")).Days)"') do (set "datecount=%%a")
goto :eof

:datecounter将被定义为计算函数
%datecount%将被定义为计算结果

需要注意的是,这样计算的是日期 1日期 2之间有几天,不要搞混
例如,3月27日到3月30日之间有2天(28,29),而不是3(28,29,30)天或4天(27,28,29,30)

如果你的电脑开机后第一次运行 PowerShell 命令,那么以上代码开机后第一次运算时会有一点延迟