Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get today's date in Windows batch environment?

Tags:

batch-file

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.

like image 713
segfault Avatar asked Aug 11 '11 10:08

segfault


3 Answers

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%.

like image 189
Alex K. Avatar answered Nov 15 '22 07:11

Alex K.


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 /?

like image 36
Aacini Avatar answered Nov 15 '22 06:11

Aacini


@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
like image 25
walid2mi Avatar answered Nov 15 '22 07:11

walid2mi