Get date in YYYYMMDD format in a Windows batch file

I need to get the date in YYYYMMDD format in a batch file.

I do this using:

set mydate=%date:~6,4%%date:~3,2%%date:~0,2% echo %mydate% 

I need it to be consistent throughout the system, even when changing the time settings.

Please inform.

+69
windows batch-file
Feb 11 '13 at
source share
2 answers

If, after reading other questions and looking at the links mentioned in the comment sections, you still cannot figure it out, continue reading.

First of all, if you go wrong, this is an offset.

It should look something like this ...

 set mydate=%date:~10,4%%date:~6,2%/%date:~4,2% echo %mydate% 

If the date was Tue 12/02/2013 , it would display as 2013/02/12 . to remove slashes, the code will look more like set mydate=%date:~10,4%%date:~7,2%%date:~4,2% , which will output 20130212

And a hint for doing this in the future, if mydate is equal to something like %date:~10,4%%date:~7,2% or the like, you probably forgot the tilde (~).

+31
Feb 12 '13 at 6:07
source share

You can try it! This should work on Windows machines.

 for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo "%%I" "%%J" "%%K" 
+2
Feb 11 '13 at 20:19
source share



All Articles