Building a date string in yyyy.mm.dd format in Batch (cmd.exe)

a walkthrough with code snippets

Tech Notes

--

To get the year, month, and day you can use the %date% environment variable and the :~ operator. %date% expands to something like Thu 08/12/2010 and :~ allows you to pick up specific characters out of a variable:

> set year=%date:~10,4%
> echo %year%
2023

> set month=%date:~4,2%
> echo %month%
01

> set day=%date:~7,2%
> echo %day%
05

This code is highly localization dependent.

--

--