I had a similar issue as the OP and I love the method using wmic @Regejok. Here is my solution to handle unique strings returned by DOS DATE (U.S. solution - sorry no other region OS to test with but this can be refactored pretty quickly for other regions).
I had servers that were returning different strings for %DATE% (date /t).
ex. 1 - Wed 07/04/2018 (perfectly fine string)
ex. 2 - 07/04/2018 (another perfectly fine string but failed my batch script because of sub-string referencing)
I was using this script selecting substrings from DATE & TIME to create date/timestamp vars for logging & archiving files:
ORIGINAL SCRIPT
@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
Please don't judge the code. :) It gets better.
RESULTS FOR 'Wed 07/04/2018':
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
This worked great with the ex. 1 string. I skipped over the abbreviated day (Wed) and all was well. Until I hit a server that returned ex. 2.
FAILED RESULTS FOR '07/04/2018':
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/
All the date/timestamps contained a '/' (fail for file naming). This is all due to the DATE string difference and the sub-string references. The TIME was fine.
Even with the failure in the month (MM) and the day (DD) strings I noticed the 2-digit and 4-digit year vars were fine (YY & CCYY). How? Negative referencing.
This became the biggest fix to my final script. Along with string replacement.
MY NEW SCRIPT
(I still love the method using wmic, this is just an alternate way allowing the 2 different return types for DATE using DOS only)
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
NOTES:
line 2:
'str' var holds the value returned from DATE.
Could be formatted like ex. 1 or ex. 2, does not matter
line 3:
this is string replacement, all '/' replaced with '' (null).
ex. 1 - 'Wed 07/04/2018' >> 'Wed 07042018', ex. 2 - '07/04/2018' >> '07042018'
line 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'
line 6:
Same with day (DD), '-6' characters from end of string, length of '2'
line 7:
2-digit year (YY) works as it did before (already used negative referencing)
line 8:
4-digit year (CCYY) works as it did before as well
line 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)
line 11:
string replacement to the rescue again, replace all spaces with '0',
turns the ' 9' to '09' (exactly what we want)
line 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
RESULTS FROM NEW SCRIPT:
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
Hope this helps. It is not unique or earth shattering, just another way.