2

I'm trying to check if today's date is after a certain date so that I can make a batch file that will only run after a certain date.

Right now I have:

@ECHO off
IF %date% GTR 24/01/2015(
ECHO it is after 24/01/2015
)
pause

But that doesn't work.

TT.
  • 15,774
  • 6
  • 47
  • 88
michael rogan
  • 39
  • 1
  • 1
  • 3
  • 1
    Not so simple -- see http://stackoverflow.com/questions/11891335/how-to-get-yesterdays-date-in-dos/14459376#14459376 – belwood Jan 25 '15 at 20:44
  • possible duplicate of [Compare 2 dates in a Windows batch file](http://stackoverflow.com/questions/17649235/compare-2-dates-in-a-windows-batch-file) – JohnLBevan Jan 25 '15 at 20:59

5 Answers5

3

In order to compare the dates you need to strip out the individual components of the current date and then put them back together into a comparable format (YYYY-MM-DD):

@ECHO OFF

SET FirstDate=2015-01-24

REM These indexes assume %DATE% is in format:
REM   Abr MM/DD/YYYY - ex. Sun 01/25/2015
SET TodayYear=%DATE:~10,4%
SET TodayMonth=%DATE:~4,2%
SET TodayDay=%DATE:~7,2%

REM Construct today's date to be in the same format as the FirstDate.
REM Since the format is a comparable string, it will evaluate date orders.
IF %TodayYear%-%TodayMonth%-%TodayDay% GTR %FirstDate% (
    ECHO Today is after the first date.
) ELSE (
    ECHO Today is on or before the first date.
)
Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33
  • I tried this one, didn't work real well. I did get it to work however with taking the right 10 characters of `%DATE%` first, then doing the year, month and day stripping. So first `SET f=%DATE%`, then `SET dt=%f:~-10%` and then working on `dt` to strip the year, month and day fields. – TT. Nov 09 '17 at 19:18
1
@if (@X)==(@Y) @end /* JScript comment
    @echo off
    set "comp_date=2015/1/20"
    rem :: the first argument is the script name as it will be used for proper help message
    for /f %%# in ('cscript //E:JScript //nologo "%~f0" "%comp_date%"') do set comp=%%#

    if %comp% EQU -1 (
        echo current date is bigger than %comp_date%
    ) else (
        echo current date is less than %comp_date%
    )

    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */

var ARGS = WScript.Arguments;
var compdate=ARGS.Item(0);

var c_date=(new Date()).getTime();
var comp_date=(new Date(compdate)).getTime();
//WScript.Echo(c_date);
//WScript.Echo(comp_date);
WScript.Echo(comp_date<c_date);
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0
@ECHO OFF
SETLOCAL
CALL :CONVERT date1 24/01/2015
CALL :CONVERT date2 %DATE%
IF %date1% gtr %date2% (ECHO earlier) ELSE (ECHO same or later)
GOTO :EOF

:CONVERT
FOR /f "tokens=1,2,3 delims=/-. " %%a IN ("%2") DO SET /a %1=%%c0000+1%%b00+1%%a-10100
GOTO :eof

But - this method will only work for date formats where the day number and month number are two digits.

I use dd/mm/yyyy for my date format. This will work as written for dd/mm/yy or dd/mm/yyyy with separators of /-. or space

If your date format has a 3-character dayname prefix, then use %date:~4% in place of %date%

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

I'd use wmic os get localdatetime and compare YYYYMMDD.

@echo off
setlocal

for /f "tokens=2 delims==" %%I in (
    'wmic os get localdatetime /format:list ^| find "="'
) do set "now=%%I"

if %now:~0,8% gtr 20150124 (
    echo It's time.
)

Then you're just comparing 8-digit integers and should get the results you expect. I'm unable to test this at the moment as I'm posting from my phone, but I don't see why it wouldn't work.

rojo
  • 24,000
  • 5
  • 55
  • 101
0

The first thing you'll want to do is open the Command Prompt. Then type the following command (and press Enter):

ECHO %DATE%

The output will probably be an abbreviated weekday name followed by the current date in the format for your locale. It may even omit the day of the week entirely.

Now just make a new text file with the extension BAT or CMD and paste in the following script. Then configure it according to the remarks (preceded by the command REM).

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

REM -- 2 digit day
SET "_day=%DATE:~-10,2%" & REM day goes first (dd/mm/yyyy); if not, remove this line
SET "_day=%DATE:~-7,2%" & REM day goes second (mm/dd/yyyy); if not, remove this line
REM -- 2 digit month
SET "_month=%DATE:~-10,2%" & REM month goes first (mm/dd/yyyy); if not, remove this line
SET "_month=%DATE:~-7,2%" & REM month goes second (dd/mm/yyyy); if not, remove this line
REM -- 4 digit year
SET "_year=%DATE:~-10,4%" & REM year goes first (yyyy/##/##); if not, remove this line
SET "_year=%DATE:~-4%" & REM year goes last (##/##/yyyy); if not, remove this line

REM -- The variables below are set to year-month-day without separators (yyyymmdd)
SET "today=%_year%%_month%%_day%" & REM today's date based on your selections above
SET "compareDate=20180727" & REM the date you are comparing with today

REM -- Here's where the magic happens with comparing the two dates
IF %compareDate% LSS %today% ECHO The comparison date is in the past.
IF %compareDate% EQU %today% ECHO The comparison date is today.
IF %compareDate% GTR %today% ECHO The comparison date is in the future.
GOTO :EOF
Matthew
  • 59
  • 6