【问题标题】:Windows Batch: How to read date and time in a file and compare it to current date and timeWindows Batch:如何读取文件中的日期和时间并将其与当前日期和时间进行比较
【发布时间】:2018-03-03 12:08:41
【问题描述】:

我目前有一个批处理脚本,可以将当前日期时间保存到日志文件中。

注意:我从微软论坛获得了这个脚本,因为我不知道如何创建批处理脚本。虽然我知道如何使用 java 进行开发,但在这种情况下我不希望这样做。

@echo off  
REM *DATETIME.BAT 
REM *Copy date and time to create a new log file 
echo.|date>datetime.tmp  
echo.|time>>datetime.tmp 
IF NOT EXIST datetime.tmp GOTO Error1 
type datetime.tmp|FIND "current">datetime.log  
rem del datetime.tmp 
GOTO End 

:Error1 
Echo.  
Echo There was an error processing the command.  
Echo Unable to find temporary sort file DATETIME.TMP.  
Echo.  
GOTO End 

:End

我可以理解这段代码是如何工作的,日志文件中的输出是这样的,例如:

The current date is: 01.01.2018 
The current time is: 13:01:17,42

我想知道的是:如何读取 another 脚本中的日期和时间并检查当前时间是否比保存的时间大 1 小时和/或当前日期是否在至少 1 天后。如果条件分别为真或不真,应该有一个特定的返回值。

我希望我能够解释我的问题。谢谢!

【问题讨论】:

  • 提供datetime.tmp 的第二行会很有用,因为您给出的示例没有向我们显示哪个是dd,哪个是mm,例如Enter the new date: (mm.dd.yy)

标签: windows date batch-file time comparison


【解决方案1】:

这是我写的,它是批处理和 vbscript 的组合(获取昨天的日期),你可以做一个if 语句并匹配如果 date == 上一个日期,你可以对时间做类似的事情。

@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
 del "%temp%\%~n0.vbs"
 set "YYYY=%result:~0,4%"
 set "MM=%result:~4,2%"
 set "DD=%result:~6,2%"
 set "result=%yyyy%-%mm%-%dd%"
 echo %result%
pause

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 2011-11-14
    • 1970-01-01
    • 2021-10-03
    • 2023-03-07
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多