批处理脚本中的日期输出错误仅在8日

问题描述 投票:0回答:2

我当前运行以下脚本来获取当前减去1和当前月份。它适用于所有日子和月份,除了每个月的8号和每年的8月。我必须将脚本更改为手动设置8月份。有谁知道为什么,有没有修复。

SET m=%date:~4,2%
SET /A m -= 1

SET m=0%m%

REM ****** SET m=08 this was used because the date was not right ******

REM SET m=08

SET currMon=%date:~4,2%/%date:~10,4%

REM ****** SET PriorMon=12/2017 this was used for Year End because the date was not right ******

REM SET PriorMon=08/2018

SET PriorMon=%m:~-2%/%date:~10,4%
batch-file batch-processing
2个回答
1
投票

这是一个混合的vb /批处理脚本。这是获取日期-1或您想要的任何天数的正确方法:

@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 "final=%dd%-%mm%-%yyyy%"
echo %final%

我简单地回应一下这里的最终结果,就今天的日期而言(对我而言,因为它是第7个)应该回应06-09-2018

您可以根据需要更改%final%的格式以适合您的日期..


0
投票

正确的日期计算纯粹的qazxsw poi但繁琐。您的方法依赖于可能未知的区域设置/用户设置相关的日期格式。

从Powershell上的Win7可以作为工具使用:

在cmd行:

are possible

在批处理文件中:

For /f "usebackq" %A in (`powershell -Nop -C "(Get-Date).AddDays(-1).ToString('MM\/yyyy')"`) Do Set Yesterday=%A

根据您的喜好修改格式字符串:

For /f "usebackq" %%A in (`
    powershell -Nop -C "(Get-Date).AddDays(-1).ToString('MM\/yyyy')"
`) Do Set Yesterday=%%A
Echo Yesterday=%Yesterday%

其他字符必须使用反斜杠进行转义。

© www.soinside.com 2019 - 2024. All rights reserved.