比较 Windows 批处理文件中的 2 个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17649235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare 2 dates in a Windows batch file
提问by Zheden
How is it possible to compare 2 dates in batch files? This:
如何比较批处理文件中的 2 个日期?这个:
if %%newdate geq %olddate% _do smth_
does not work.
不起作用。
In this case I have that27.05.2013
is greater than15.07.2013
and14.07.2013
is less than15.07.2013
在这种情况下,我有一个27.05.2013
是大于15.07.2013
并且14.07.2013
是小于15.07.2013
Zhenya
振亚
回答by Endoro
try this:
尝试这个:
set "sdate1=%olddate:~-4%%olddate:~3,2%%olddate:~0,2%"
set "sdate2=%newdate:~-4%%newdate:~3,2%%newdate:~0,2%"
if %sdate1% GTR %sdate2% (goto there) else echo here
回答by npocmaka
call :date_to_number %date1% date1
call :date_to_number %date2% date2
if %date1% GEQ %date2% echo date1 is bigger
goto :eof
:date_to_number
setlocal
if "%~1" EQU "" goto :eof
for /f "tokens=1,2,3 delims=." %%D in ("%~1") do (
set "the_date=%%F%%E%%D"
)
endlocal & if "%~2" neq "" (set %~2=%the_date%) else echo %the_date%
goto :eof
But this will work only with dates in format DD.MM.YYYY
但这仅适用于格式为 DD.MM.YYYY 的日期
回答by Jon Dresser
I had an issue where I wanted to find users who's passwords had expired, or were about to expire.
我遇到了一个问题,我想查找密码已过期或即将过期的用户。
So for starters, Windows 10 has changed the format of the %date% variable so it looks different than previous versions. therefore, the output of "echo %date%" looks like: "Thu 05/09/19" so I use this one liner to take today's date and convert it to yyyymmdd format.
因此,对于初学者来说,Windows 10 更改了 %date% 变量的格式,因此它看起来与以前的版本不同。因此,“echo %date%”的输出看起来像:“Thu 05/09/19”,所以我使用这一行来获取今天的日期并将其转换为 yyyymmdd 格式。
set today=20%date:~-2%%date:~4,2%%date:~7,2%
(today = 20190509)
(今天 = 20190509)
this is easy because we know that there is exactly 2 digits for each month and day.
这很容易,因为我们知道每个月和日正好有 2 位数字。
Now, if the "date format" that you are working from does not pad the single digit months and days with a preceding zero, you will have to to pad them yourself. For example, the output of the command: net user %username% /domain looks like "5/9/19", note the missing zeros...
现在,如果您使用的“日期格式”没有用前面的零填充个位数的月份和日期,您将不得不自己填充它们。例如,命令的输出:net user %username% /domain 看起来像“5/9/19”,注意缺少的零...
So you can extract this a couple different ways. the simplest way is to grab each field seperately, drop a zero in front of the month and day variables, and then grab only the last two digits of them. that way 5 becomes 05, and 12 becomes 012, and then goes back to 12.
所以你可以通过几种不同的方式提取它。最简单的方法是分别抓取每个字段,在月份和日期变量前面放一个零,然后只抓取它们的最后两位数字。这样5变成05,12变成012,然后又回到12。
for /f "tokens=3-5 delims=/ " %%a in ('net user %username% /domain ^|find /i "Password expires"') do (
set mm=0%%a& set dd=0%%b& set yyyy=20%%c
set expires=!yyyy!!mm:~-2!!dd:~-2!
if NOT !expires! GTR %today% echo Password has expired and needs to be changed.
回答by Tim
You should always use the ISO format for dates (yyyy-MM-dd HH:mm:ss). This makes it unambiguous, locale agnostic, they sort chronologically and you can easily compare them as strings in CMD files.
您应该始终使用 ISO 格式的日期 (yyyy-MM-dd HH:mm:ss)。这使得它明确无误,与区域设置无关,它们按时间顺序排序,您可以轻松地将它们作为 CMD 文件中的字符串进行比较。
For /F "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE') do (if .%%i EQU .LocalDateTime set ldt=%%j)
set CURRENT_DATE=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
set CURRENT_TIME=%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,2%
This will give you:
这会给你:
CURRENT_DATE in yyyy-MM-dd format
CURRENT_TIME in HH:mm:ss format
回答by Grog Klingon
I create a simple c program i call newest.exe. Below is the source code.
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
// syntax: newest file1 file2
//
// if age1 = age2, prints 0 age1 age2
// if age1 < age2, prints 1 age1 age2
// if age1 > age2, prints 2 age1 age2
// where
// age1 is age of file1 is seconds.
// age2 is age of file2 is seconds.
//
// To capture the output in a DOS bat file use:
// for /f "tokens=1-3" %%i in ('%srcPath%\newest.exe %src% %des%') do (
// set newest=%%i
// set age1=%%j
// set age2=%%k
// )
int main(int argc, char **argv)
{
struct stat fileStat;
time_t now;
long age1, age2;
if(argc != 3)
{
printf("syntax: %s File1 File2\n", argv[0]);
return 1;
}
now = time(NULL);
if(stat(argv[1],&fileStat) < 0) return 1;
age1 = now - fileStat.st_mtime;
if(stat(argv[2],&fileStat) < 0) return 1;
age2 = now - fileStat.st_mtime;
if( age1 == age2 )
printf("0 %d %ld\n", age1, age2);
else if( age1 < age2 )
printf("1 %d %ld\n", age1, age2);
else
printf("2 %d %ld\n", age1, age2);
return 0;
}