Tuesday, February 26, 2013

The Date Command... I mean Environmental Variable


It clicked for me how the date command environmental variable works in Windows. Seems like such a simple idea, and for all the information on the Internet about it no one explains it like this (at least not that I've found). Most only show what to type to get a desired result.

Let's say you run md %date:~10,4% from a command prompt. You just made a folder named 2013. Unless you run that same command a year from now, then you'll make a folder named 2014. This can get complicated quick. Consider:
z:\logs\%Date:~-4,4%-%Date:~-10,2%-%Date:~-7,2%_Server1.txt
This is a file at "z:\logs\2013-02-26_Server1.txt". Well, for today anyway.

At it's simplest you can copy and paste what you need below (why not that's what the rest of the Internet tells you to do--if you want to know why these work don't skip the last section).
  • Four Digit Year     %Date:~-4,4%
  • Two Digit Month    %Date:~-10,2%
  • Two digit Day      %Date:~7,2%
Note that in the considered example these chunks of text are separated by dashes '-' which is what makes the date look the way it does. Without them it would just be a jumble of numbers, 20130226, which is a personal preference. You could use slashes '/' if this didn't deal with a file name (slashes are invalid characters for a file name).


%Inside the Percent Symbols%


For those of use that like to know why things work open a dos prompt and type date /t. The output will be something like this:
Tue 02/26/2013
Calling %Date:~-4,4%, which can also be done from the command prompt by typing echo %Date:~-4,4%, will return the last four digits of the above date sting. Negative 4 means to start four places from the end, then the 4 tells it to go four  places to the right: which is where the year is kept. %Date:~10,4% will return the same thing, the difference is the command starts from the left, moves 10 places to the right, then grabs the next four places. The same is true for the month and day.

You can return anything in that 14 digit string, we only exclude the slashes in file operations because of name limitations. For kicks you could run echo %Date:~1,6% from the command prompt to get back "ue 02/2". If you had a need for that for whatever reason. Note the first place in the string is 0 not 1. To get the three letter day of the week would require echo %Date:~0,3%.

To learn more search results may be misleading if you are looking for help with "dos date", try a search on terms like "dos string manipulation". That pointed me in the right direction.


Bonus: Why am I using some goofy date format?

It's sortable.

Let's face it, Month/Day/Year just doesn't make sense. It's out of order. I could see an argument for Day/Month/Year, but that still lacks logical sortability. 

http://en.wikipedia.org/wiki/ISO_8601

EDIT: 2013-05-09 (see what I did there), It was pointed out to me there is a difference between the command date and the environmental variable %date%. This post deals with the environmental variable not the command.