0

I am trying to compare a fixed date with current system date. I want to check if the current date is greater or same than fixed date. Example:

@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"

Output:

"do nothing"
"yes run it"

Above code isn't working. Any suggestion in where I am doing mistake? Thanks in advance :)

Edit:

I tried like this:

@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"

Didn't work either.

Output:

"yes run it"
Jibon
  • 81
  • 8
  • 1
    Possible duplicate of [Compare 2 dates in a Windows batch file](https://stackoverflow.com/questions/17649235/compare-2-dates-in-a-windows-batch-file) – HackSlash Jul 05 '18 at 15:00
  • 1
    Could also be duplicate of: https://stackoverflow.com/questions/15670666/compare-2-dates-in-a-batch-file – HackSlash Jul 05 '18 at 15:00
  • @HackSlash I tried but didn't work for me. – Jibon Jul 05 '18 at 15:20
  • How can the current date ever be greater than a future date? Doesn't seem logically possible. – Squashman Jul 05 '18 at 15:31
  • 1
    Your question doesn't require a batch file or any code because a future date by definition always has to be greater than the current date! Also @Jibon, if you try the code suggested in the comments, then please [edit your question](https://stackoverflow.com/posts/51194469/edit) again to include it, along with a full explanation of what happened when you ran it. – Compo Jul 05 '18 at 15:31
  • 1
    @Compo, sorry for that. Actually it will be compare current date with a fixed date. If current date is greater than or same as the fixed date then one command will be run. – Jibon Jul 05 '18 at 15:57
  • Of course it is going to run the `:there` label. Batch files process their code line by line. The only way to skip over lines is to use a `GOTO` like you did with your `IF` commands. – Squashman Jul 05 '18 at 15:57
  • This code presumes that the `%DATE%` variable is in `dd-MM-yyyy` format. That might not be correct on another machine. Use `ECHO %DATE%` to see the current format. – lit Jul 05 '18 at 16:00
  • @lit you are correct the output it `05-Jul-18` – Jibon Jul 05 '18 at 16:02
  • @Jibon - That would suggest that your parsing of the current date is not correct. The format appears to be `dd-MMM-yyyy`. – lit Jul 05 '18 at 16:03
  • @lit so, Do I need to change `fixedDate=07-Jul-18`? I mean same format as current date? – Jibon Jul 05 '18 at 16:08
  • @Jibon, batch files know absolutely nothing about variable types which is pretty evident because most languages require you to declare the variables data type. Everything is a character string in batch files. So you have to put the date into a YYYYMMDD format to determine if one is larger than the other. – Squashman Jul 05 '18 at 16:09

1 Answers1

2

You can use PowerShell which understands dates.

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

Alternatively, you could do the command in the 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)
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thanks for your solution. I would like to run a command. How can I do that? `if %COMPRESU% == "true" (echo here) else (echo there)` – Jibon Jul 05 '18 at 16:35