在批处理文件中,如何检查今天的日期是否晚于设定的日期?
问题描述:
我正在尝试检查今天的日期是否在特定日期之后,以便我可以制作一个仅在特定日期之后运行的批处理文件.
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.
现在我有:
@ECHO off
IF %date% GTR 24/01/2015(
ECHO it is after 24/01/2015
)
pause
但这不起作用.
答
为了比较日期,您需要去除当前日期的各个组成部分,然后将它们放回可比较的格式中( YYYY-MM-DD
):
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.
)