Get Date in YYYYMMDD format in windows batch file

WindowsBatch File

Windows Problem Overview


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

I am doing this using :

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

I need it to be consistent across system, even on changing the time settings.

Please advise.

Windows Solutions


Solution 1 - Windows

If, after reading the other questions and viewing the links mentioned in the comment sections, you still can't figure it out, read on.

First of all, where you're going wrong is the offset.

It should look more like this...

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

If the date was Tue 12/02/2013 then it would display it as 2013/02/12.

To remove the slashes, the code would look more like

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

which would output 20130212

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

Solution 2 - Windows

You can try this ! This should work on windows machines.

for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo "%%I" "%%J" "%%K"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2061002View Question on Stackoverflow
Solution 1 - WindowsBDMView Answer on Stackoverflow
Solution 2 - WindowsGaurav Kolarkar_InfoCeptsView Answer on Stackoverflow