我正在尝试检查今天的日期是否在特定日期之后,以便我可以制作一个仅在特定日期之后运行的批处理文件。
现在我有:
@ECHO off
IF %date% GTR 24/01/2015(
ECHO it is after 24/01/2015
)
pause
但这不起作用。
答案 0 :(得分:1)
为了比较您需要删除当前日期的各个组件所需的日期,然后将它们重新组合成可比较的格式(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.
)
答案 1 :(得分:0)
@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);
答案 2 :(得分: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
但是 - 此方法仅适用于日期编号和月份编号为两位数的日期格式。
我使用dd/mm/yyyy
作为我的日期格式。这将按dd/mm/yy
或dd/mm/yyyy
编写的方式使用/-. or space
如果您的日期格式具有3个字符的日期名称前缀,请使用%date:~4%
代替%date%
答案 3 :(得分:0)
我使用wmic os get localdatetime
并比较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.
)
然后你只是比较8位整数,并且应该得到你期望的结果。我现在无法对此进行测试,因为我在手机上发帖,但我不知道为什么它无法正常工作。
答案 4 :(得分:0)
您要做的第一件事是打开命令提示符。然后键入以下命令(并按Enter):
ECHO %DATE%
输出可能是缩写的工作日名称,后跟当前日期(以您的语言环境格式)。甚至可能会完全忽略一周中的某天。
现在,只需创建一个扩展名为BAT或CMD的新文本文件,然后粘贴以下脚本即可。然后根据备注(以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