我正在写一个批处理文件来复制一些数据,我遇到的问题是,我想在变量中捕获日期,以便我可以将它用作文件名。我希望它的格式为dd-mm-yyyy。
我找到了这个示例here,但我无法将其转换为我需要的格式。
答案 0 :(得分:2)
此解决方案与区域设置无关:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
它会给你(独立于区域设置!):
20130802203023.304000+120
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC> )
从这里开始,很容易:
set mydate=%datetime:~6,2%-%datetime:~4,2%-%datetime:~0,4%
echo %mydate%
答案 1 :(得分:0)
解决方案
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
答案 2 :(得分:0)
:date
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is %dateday% %datemonth% %dateyear%, sir.
exit /B 0
:xpdate
echo It is %date%, sir.
%speech% "It is %date%, sir."
exit /B 0
:w7dat
if %time:~0,-9% GTR 12 (set /a timehour=%time:~0,-9%-12) else set timehour=%time:~0,-9%
if %time:~3,1%==0 (set timeminute=%time:~4,1%) else set timeminute=%time:~3,2%
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is currently %timeminute% minutes past %timehour% on %dateday% %datemonth% %dateyear%, sir.
exit /B 0
这是我在我的批处理AI中使用的只需更改:date:w7dat以及所有那些你需要的东西
答案 3 :(得分:0)
这是一个使用PowerShell的解决方案,它几乎在所有版本的Windows上预先安装,并且比批处理功能强大得多。
Get-Date -Format 'yyyy-MM-dd'
for /f %%a in ('powershell -nop -c "Get-Date -Format 'yyyy-MM-dd'"') do set newdate=%%a