Windows批处理将固定日期与当前日期进行比较

时间:2018-07-05 14:57:36

标签: batch-file cmd

我正在尝试将固定日期与当前系统日期进行比较。我想检查当前日期是否大于或等于固定日期。示例:

@ECHO OFF
set fixedDate=27-09-2018
set current=%date%

if %current% GEQ %fixedDate% (goto there)  else (goto here)

:here
echo "do nothing"

:there
echo "yes run it"

输出:

"do nothing"
"yes run it"

以上代码无效。关于我在哪里做错的任何建议?在此先感谢:)

编辑:

我尝试过这样:

@ECHO OFF
set FixedDate=27-09-2018
set current=%date%

set "sdate1=%current:~-4%%current:~3,2%%current:~0,2%"
set "sdate2=%FixedDate:~-4%%FixedDate:~3,2%%FixedDate:~0,2%"

if %sdate1% GEQ %sdate2% (goto there)  else (goto here)

:here
echo "do nothing"

:there
echo "yes run it"

也不起作用。

输出:

"yes run it"

1 个答案:

答案 0 :(得分:1)

您可以使用了解日期的PowerShell。

SET "REFDATE=2018-07-04"
FOR /F %%a IN ('powershell -NoProfile -Command "([Datetime]'%REFDATE%') -gt (Get-Date)"') DO (SET "COMPRESU=%%a")
ECHO %COMPRESU%

IF /I "%COMPRESU%" == "True" (GOTO DoTrue) ELSE (GOTO DoFalse)

:DoTrue
ECHO Doing TRUE
GOTO AfterDateTest

:DoFalse
ECHO Doing FALSE

:AfterDateTest

或者,您可以在if中执行命令。

FOR /F %%a IN ('powershell -NoProfile -Command "([Datetime]'%REFDATE%') -gt (Get-Date)"') DO (SET "COMPRESU=%%a")
IF /I "%COMPRESU%" == "true" (truecmd.exe) ELSE (falsecmd.exe)