【问题标题】:Compare 2 dates in a Windows batch file比较 Windows 批处理文件中的 2 个日期
【发布时间】:2013-07-15 07:52:11
【问题描述】:

如何比较批处理文件中的两个日期? 这个:

if %%newdate geq %olddate%  _do smth_

不起作用。

在这种情况下,我有
27.05.2013 大于 15.07.2013
14.07.2013 小于 15.07.2013 p>

镇雅

【问题讨论】:

标签: windows date if-statement batch-file cmd


【解决方案1】:

试试这个:

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

【讨论】:

  • 我认为,当使用引号时,它会强制进行 ascii 比较,因此您应该使用裸变量 if %sdate1% GTR %sdate2% echo %olddate% is greater than %newdate%
  • @foxidrive 是的,您在数字比较方面是对的。但我认为在这种特殊情况下,这并没有什么不同。尽管如此,我还是做了一个编辑。我之前并没有考虑太多:)数字比较更好更安全。
  • 你说得对,在这种情况下没关系。事实上,如果数字非常大,那么在超出数学限制时引用它们甚至是有好处的。
  • 此技术取决于使用的日期格式 dd/MM/yyyy
【解决方案2】:

您应该始终使用 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%

这会给你:

CURRENT_DATE in yyyy-MM-dd format
CURRENT_TIME in HH:mm:ss format

【讨论】:

    【解决方案3】:
    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
    

    但这仅适用于格式为 DD.MM.YYYY 的日期

    【讨论】:

      【解决方案4】:

      我遇到了一个问题,我想查找密码已过期或即将过期的用户。

      因此,对于初学者来说,Windows 10 更改了 %date% 变量的格式,使其看起来与以前的版本不同。因此,“echo %date%”的输出如下所示: “2019 年 5 月 9 日星期四” 所以我使用这一行来获取今天的日期并将其转换为 yyyymmdd 格式。

      set today=20%date:~-2%%date:~4,2%%date:~7,2%
      

      (今天 = 20190509)

      这很容易,因为我们知道每个月和天正好有 2 位数字。

      现在,如果您正在使用的“日期格式”没有用前面的零填充单个数字月份和日期,则您必须自己填充它们。 例如,命令的输出:net user %username% /domain 看起来像“5/9/19”,注意缺少的零...

      所以你可以用几种不同的方式来提取它。最简单的方法是分别抓取每个字段,在月份和日期变量前面放一个零,然后只抓取它们的最后两位数。 这样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. 
      

      【讨论】:

      【解决方案5】:
      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 1970-01-01
        相关资源
        最近更新 更多