Why do people make this so hard? I see huge scripts on the internet dedicated to just this topic of parsing the date. Here, it’s this simple:
First, let’s look what date looks like in DOS:
C:UsersAdmin>date /t Tue 06/12/2012
With that in mind, you can also use the built-in %DATE% variable:
C:UsersAdmin>echo %DATE% Tue 06/12/2012
Now that we have this information, let’s look at how to simplify this and come up with whatever format we want to display the date in…
C:UsersAdmin>echo %date:~-4,4%%date:~-7,2%%date:~-10,2% 20121206
String substitution; Read more about it here: DosTips
So you have replaced something like this:
@For /F tokens=1,2,3 delims=. %%A in ('Date /t') do @( Set Day=%%A Set Month=%%B Set Year=%%C ) @echo DAY = %Day% @echo Month = %Month% @echo Year = %Year%
With a single, simple, line!
If you want to experiment further, try using %TIME%
too. I use %DATE%
and %TIME%
in my backup/compression batch scripts for pre/post backup solutions in many cases and also for log files. Works like a champ.