批次:如何导出昨天的日期保留格式[mm / dd / yyyy]?

时间:2019-12-05 00:25:23

标签: date batch-file environment-variables

我正在尝试为昨天的日期创建一个环境变量。该格式必须为 MM / DD / YYYY

对于我目前拥有的代码:

 set m=%date:~-7,2%
 set /A m -= 1
 set DATE_YESTERDAY=%date:~-10,2%/%m%/%date:~-4,4%
 echo %DATE_YESTERDAY%

但是,当我用-1(而不是“ 12/03/2019 ””代替昨天的日子时,效果很好,我得到的是“ 2019/12/3 ”) 。因此,缺少零/ 0。

是否有任何格式保持为 MM / DD / YYYY 的想法?

关于此,我还看到了许多其他问题,所有这些问题都很简短!

注意-我不需要Powershell或VBS脚本。我知道只需批处理即可完成。

2 个答案:

答案 0 :(得分:2)

您应该做的是使用一种不基于用户/区域设置/ PC设置的方法。通常使用,但这不是最快的方法,仍然需要执行一些数学运算。

您可以使用直接嵌入中的

<!-- :
@Echo Off
For /F %%# In ('CScript //NoLogo "%~f0?.wsf"') Do Set "YesterDate=%%#"
Echo(%YesterDate%
Pause
GoTo :EOF
-->
<Job><Script Language="VBScript">
    dtmYesterday = DateAdd("d", -1, Now())
    strDate = Right("0" & Month(dtmYesterday), 2) _
      & "/" & Right("0" & Day(dtmYesterday), 2) _
      & "/" & Year(dtmYesterday)
    WScript.Echo strDate
  </Script></Job>


如果愿意,也可以使用中的代替:

@Echo Off
For /F %%# In ('PowerShell -NoP "(Get-Date).AddDays(-1).ToString('MM/dd/yyy')"'
)Do Set "YesterDate=%%#"
Echo(%YesterDate%
Pause

答案 1 :(得分:1)

Windows命令处理器cmd.exe不具有对日期计算的内置支持。 Windows上默认安装的其他脚本解释器,例如VBScript或PowerShell支持日期计算以及使用Compo发布的使用VBScript或PowerShell的解决方案。

这里是一个纯批处理文件解决方案,用于从当前日期开始计算昨天的日期,并带有解释代码的注释。可以删除带有注释命令rem的行,以便Windows命令处理器更快地处理批处理文件。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" (
    rem Get local date and time in a region independent format.
    for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /VALUE') do set "LocalDateTime=%%I"
) else (
    rem This is for fast testing determining the date of yesterday from any
    rem date specified as parameter in format yyyyMMdd on calling this batch
    rem file from within a command prompt window. The parameter string is
    rem not validated at all as this is just for testing the code below.
    set "LocalDateTime=%~1"
)

rem Get day, month and year from the local date/time string (or parameter).
set "Day=%LocalDateTime:~6,2%"
set "Month=%LocalDateTime:~4,2%"
set "Year=%LocalDateTime:~0,4%"

rem Define a variable with today's date in format MM/dd/yyyy.
set "Today=%Month%/%Day%/%Year%"

rem Decrease the day in month by 1 in any case.

rem It is necessary to remove leading 0 for the days 08 and 09 as
rem those two days would be otherwise interpreted as invalid octal
rem numbers and decreased result would be -1 instead of 7 and 8.
rem if "%Day:~0,1%" == "0" set "Day=%Day:~1%"
rem set /A Day-=1

rem Faster is concatenating character 1 with the day string to string
rem representing 101 to 131 and subtract 101 to decrease day by one.
set /A Day=1%Day%-101

rem The yesterday's date is already valid if the day of month is greater 0.
if %Day% GTR 0 goto BuildYesterday

rem Yesterday is in previous month if day is equal (or less than) 0.
rem Therefore decrease the current month by one with same method as
rem described above to decrease correct also the months 08 and 09.
set "Day=31"
set /A Month=1%Month%-101

rem Yesterday is in previous year if month is equal (or less than) 0.
if %Month% GTR 0 goto GetLastDay
set /A Year-=1
set "Month=12" & goto BuildYesterday

:GetLastDay
rem Determine last day of month depending on month.
for %%I in (4 6 9 11) do if %Month% == %%I set "Day=30" & goto BuildYesterday
if not %Month% == 2 goto BuildYesterday

rem Determine if this year is a leap year with 29 days in February.
set /A LeapYearRule1=Year %% 400
set /A LeapYearRule2=Year %% 100
set /A LeapYearRule3=Year %% 4

rem The current year is always a leap year if it can be divided by 400
rem with 0 left over (1600, 2000, 2400, ...). Otherwise if the current
rem year can be divided by 100 with 0 left over, the current year is NOT
rem a leap year (1900, 2100, 2200, 2300, 2500, ...). Otherwise the current
rem year is a leap year if the year can be divided by 4 with 0 left over.
rem Well, for the year range 1901 to 2099 just leap year rule 3 would be
rem enough and just last IF condition would be enough for this year range.

set "Day=28"
if LeapYearRule1 == 0 goto BuildYesterday
if NOT %LeapYearRule2% == 0 if %LeapYearRule3% == 0 set "Day=29"

rem The leading 0 on month and day in month could be removed and so both
rem values are defined again as string with a leading 0 added and next just
rem last two characters are kept to get day and month always with two digits.

:BuildYesterday
set "Day=0%Day%"
set "Day=%Day:~-2%"
set "Month=0%Month%"
set "Month=%Month:~-2%"

rem Define a variable with yesterday's date in format MM/dd/yyyy.
set "Yesterday=%Month%/%Day%/%Year%"

echo     Today is: %Today%
echo Yesterday is: %Yesterday%

endlocal

请阅读我在Why does %date% produce a different result in batch file executed as scheduled task?上的答案,它详细说明了 FOR 命令行,该命令行使用 WMIC 以独立于区域的格式获取当前日期。

这是上面的代码,其中没有注释,空行和仅用于测试代码的第一个 IF 条件。 year年标识也进行了优化,仅使用第三条规则,这意味着该代码仅在1901年至2099年有效,应该足够。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /VALUE') do set "LocalDateTime=%%I"
set "Day=%LocalDateTime:~6,2%" & set "Month=%LocalDateTime:~4,2%" & set "Year=%LocalDateTime:~0,4%"
echo     Today is: %Month%/%Day%/%Year%
set /A Day=1%Day%-101
if %Day% GTR 0 goto BuildYesterday
set "Day=31"
set /A Month=1%Month%-101
if %Month% GTR 0 goto GetLastDay
set /A Year-=1
set "Month=12" & goto BuildYesterday
:GetLastDay
for %%I in (4 6 9 11) do if %Month% == %%I set "Day=30" & goto BuildYesterday
if not %Month% == 2 goto BuildYesterday
set /A LeapYearRule3=Year %% 4
if %LeapYearRule3% == 0 (set "Day=29") else set "Day=28"
:BuildYesterday
set "Day=0%Day%"
set "Day=%Day:~-2%"
set "Month=0%Month%"
set "Month=%Month:~-2%"
echo Yesterday is: %Month%/%Day%/%Year%
endlocal

要了解所使用的命令及其工作方式,请打开command prompt窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

有关用于在单个命令行上指定多个命令的运算符&的说明,另请参见single line with multiple commands using Windows batch file