I would like to get today's date in the format of YYYYMMDD in Windows batch environment, but don't know where to start or what to do.
Any code or direction is appreciated.
On my system, where echo %date%
returns dd/mm/yyyy
:
set now=%date:~6,4%%date:~3,2%%date:~0,2%
echo.%now%
The syntax used is %date:~S,L%
where S
is a character offset and L
is the length to read from the value returned by %date%
.
You may also use a FOR command to separate the parts of a date:
for /f "tokens=1-3 delims=/" %%a in ("%date%") do set now=%%c%%a%%b
Date components are split by / (delims) and taken the first three parts (tokens) in variable %%a and successive ones (%%b and %%c).
Although this seems more complicated than the former method, it is less prone to get errors when you used it. For further details, type: FOR /?
@echo off
for /f "tokens=*" %%a in ('
"wmic path Win32_LocalTime get year,month,day /value|findstr ="
') do @set %%a
echo %year%%month%%day%
pause
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With