我遇到了与 OP 类似的问题,我喜欢使用 wmic @Regejok 的方法。这是我处理 DOS DATE 返回的唯一字符串的解决方案(美国解决方案 - 抱歉,没有其他区域的操作系统可供测试,但可以很快为其他区域重构)。
我的服务器为 %DATE% (date /t) 返回了不同的字符串。
例如。 1 - Wed 07/04/2018(完美的细绳)
例如。 2 - 07/04/2018(另一个完美的字符串,但由于子字符串引用而使我的批处理脚本失败)
我正在使用此脚本从 DATE 和 TIME 中选择子字符串来创建用于记录和归档文件的日期/时间戳变量:
原始脚本
@echo off
set HH=%TIME:~0,1%
if "%HH%"==" " goto addzero
goto hourOK
:addzero
set HH=0%TIME:~1,1%
goto end
:hourOK
set HH=%TIME:~0,2%
:end
set mn=%TIME:~3,2%
set SS=%TIME:~6,2%
set ms=%TIME:~9,2%
set MM=%DATE:~4,2%
set DD=%DATE:~7,2%
set YY=%DATE:~-2%
set CCYY=%DATE:~-4%
set dts=%CCYY%%MM%%DD%%HH%%mn%%SS%%ms%
set dateonly=%CCYY%%MM%%DD%
set monthlog=%CCYY%%MM%
@echo on
请不要评判代码。 :) 它变得更好了。
“2018 年 7 月 4 日星期三”的结果:
C:\>set HH=
C:\>if " " == " " goto addzero
C:\>set HH=09
C:\>goto end
C:\>set mn=30
C:\>set SS=49
C:\>set ms=69
C:\>set MM=07
C:\>set DD=04
C:\>set YY=18
C:\>set CCYY=2018
C:\>set dts=2018070409304969
C:\>set dateonly=20180704
C:\>set monthlog=201807
这对前任很有效。 1 个字符串。我跳过了简短的一天(星期三),一切都很好。直到我遇到返回 ex 的服务器。 2.
“2018 年 7 月 4 日”的失败结果:
C:\>set HH=
C:\>if " " == " " goto addzero
C:\>set HH=09
C:\>goto end
C:\>set mn=31
C:\>set SS=48
C:\>set ms=59
C:\>set MM=4/
C:\>set DD=01
C:\>set YY=18
C:\>set CCYY=2018
C:\>set dts=20184/0109314859
C:\>set dateonly=20184/01
C:\>set monthlog=20184/
所有日期/时间戳都包含一个“/”(文件命名失败)。这都是由于 DATE 字符串差异和子字符串引用造成的。时间很好。
即使月份 (MM) 和日期 (DD) 字符串出现故障,我注意到 2 位数和 4 位数年份变量都很好(YY 和 CCYY)。如何?负引用。
这成为我最终脚本的最大修复。连同字符串替换。
我的新脚本
(我仍然喜欢使用 wmic 的方法,这只是允许 DATE 仅使用 DOS 的 2 种不同返回类型的另一种方法)
01 @echo off
02 set str=%DATE%
03 set str=%str:/=%
04
05 set MM=%str:~-8,2%
06 set DD=%str:~-6,2%
07 set YY=%str:~-2%
08 set CCYY=%str:~-4%
09
10 set HH=%TIME:~0,2%
11 set HH=%HH: =0%
12 set mn=%TIME:~3,2%
13 set SS=%TIME:~6,2%
14 set ms=%TIME:~9,2%
15
16 set _dts=%CCYY%%MM%%DD%%HH%%mn%%SS%%ms%
17 set _dateonly=%CCYY%%MM%%DD%
18 set _monthlog=%CCYY%%MM%
19 @echo on
注意事项:
第 2 行:
'str' var holds the value returned from DATE.
Could be formatted like ex. 1 or ex. 2, does not matter
第 3 行:
this is string replacement, all '/' replaced with '' (null).
ex. 1 - 'Wed 07/04/2018' >> 'Wed 07042018', ex. 2 - '07/04/2018' >> '07042018'
第 5 行:
this is where negative referencing made both date strings equal (in a sense),
month (MM) starts at '-8' characters from the end of the string in both
examples, length of '2'
第 6 行:
Same with day (DD), '-6' characters from end of string, length of '2'
第 7 行:
2-digit year (YY) works as it did before (already used negative referencing)
第 8 行:
4-digit year (CCYY) works as it did before as well
第 10 行:
TIME always returns 2-digits in its hour (HH) position, it just doesn't have
a leading '0' if less than 10, what it does return is a leading space
i.e. %TIME:~0,2% will return ' 9' for 9:00 AM (not desired for date/timestamps)
第 11 行:
string replacement to the rescue again, replace all spaces with '0',
turns the ' 9' to '09' (exactly what we want)
第 12 行:
minutes, seconds, milliseconds all work as they did previously, using
sub-strings to avoid the colons (':') & the period ('.') in the time stamp
i.e. 10:01:26.29
新脚本的结果:
C:\>set str=07/04/2018
C:\>set str=07042018
C:\>set MM=07
C:\>set DD=04
C:\>set YY=18
C:\>set CCYY=2018
C:\>set HH= 9
C:\>set HH=09
C:\>set mn=42
C:\>set SS=26
C:\>set ms=48
C:\>set _dts=2018070409422648
C:\>set _dateonly=20180704
C:\>set _monthlog=201807
C:\>set str=Wed 07/04/2018
C:\>set str=Wed 07042018
C:\>set MM=07
C:\>set DD=04
C:\>set YY=18
C:\>set CCYY=2018
C:\>set HH=10
C:\>set HH=10
C:\>set mn=50
C:\>set SS=55
C:\>set ms=48
C:\>set dts=2018070410505548
C:\>set dateonly=20180704
C:\>set monthlog=201807
希望这会有所帮助。它不是独一无二的,也不是惊天动地的,只是另一种方式。