Windows 批量比较固定日期与当前日期

问题描述 投票:0回答:2

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

@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"
batch-file cmd
2个回答
2
投票

您可以使用理解日期的 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)

0
投票

抱歉来晚了。但如果仍然实际,请参阅下一步。 你的第二个例子几乎是正确的!尝试在 IF 中添加 /I 即可。所以正确的代码是:

@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 /I %sdate1% GEQ %sdate2% (goto there)  else (goto here)

:here
echo "do nothing"

:there
echo "yes run it"
© www.soinside.com 2019 - 2024. All rights reserved.