Windows Server 2008中的.bat文件%date%错误输出

时间:2017-06-16 08:06:08

标签: batch-file windows-server-2008

我正在使用%date:~10%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%作为日期

输出应该是:201706161102

但输出:016.1102

怎么了?

1 个答案:

答案 0 :(得分:1)

我建议您使用WMIC命令的输出:

@echo off
Title Get Date and Time with WMIC
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
echo YYYYMMDDHHMinSec
Set "DateTimeVar=%YYYY%%MM%%DD%%HH%%Min%%Sec%"
echo %DateTimeVar%
pause