如何在命令提示符下以数字方式更改变量

时间:2013-09-13 05:42:39

标签: date cmd variable-assignment

我使用此代码使用了%date%的系统日期     设d = 20%日期:~6,2 %%日期:〜0.2% 它的输出是201309 如何在30天之前得到相同的信息(所以我想要201308)?比如是否有日期的添加/减法等操作?我试过了     设置/ a“date2 =%date%-30” 但是它给出了错误:“无效的数字。数字常量是十进制的(17), 十六进制(0x11)或八进制(021)。“如果cmd以0开头,则该值为十六进制。

1 个答案:

答案 0 :(得分:0)

使用此批处理文件,您应该得到所需内容:

例程可以进行一些额外的日期数学运算 - 批处理文件位于前9行之后,可以自包含。

@echo off
call datebatch today -30
set "var=%day:0,6%"
echo "%var%"
pause
goto :eof
:datebatch
:: Date foward & backward
@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%