在批处理文件中,如何检查今天的日期是否在设置的日期之后? [英] In a batch file, how would I check if today's date is after a set date?

查看:177
本文介绍了在批处理文件中,如何检查今天的日期是否在设置的日期之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查今天的日期是否在特定日期之后,以便我可以制作一个仅在特定日期之后运行的批处理文件.

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.
)

这篇关于在批处理文件中,如何检查今天的日期是否在设置的日期之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆