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.

20 comments:

Anonymous said...

I don't know if it's significant or not, but this is posted the day before xkcd.com/1179/

James said...

I noticed that too. My grandmother would say great minds think alike. Most of what Randall posts are way over my head, so I call it dumb luck.

Anonymous said...

actually, don't confuse the date command with the DATE environment variable. the second is what you are using in the examples.
also, be aware that the date format is, unfortunately, locale dependent *and* there is no good way (that i know of) to check the current locale in batch script. :/

James said...

Thanks for pointing that out Anonymous, I am going to have to read up on that. Most of the post I make here are to help me remember how I did something, but I like to be accurate.

I should change the post name lol.

KBO분석 제이리그순위 said...

I like what you guys are up also. Such clever work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my web site

스페셜토토 스포츠TOTO 스포츠배팅게임 와이즈토토추천

Majortotosite Top said...

That’s a great article! The neatly organized content is good to see.

Oncasinosite Net said...

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time.

Totopick Pro said...

Will surely come and visit this blog more often. Thanks for sharing.

고스톱 said...

Your article has answered the question I was wondering about!

카지노사이트랭크 said...

This website is very useful and beautiful. thank you.

스포츠토토맨 said...

Please visit our website as well. be successful and victorious

19가이드03 said...

thanks for sharing this type of useful article to read, keep it up!

카지노사이트 said...

this piece of writing is pleasant and very informative.

스포츠토토링크 said...

finally i found best reading blog site in google, thanks

Steve Williams said...

The post about the environmental variable is very useful and effective by using the command prompt. Keep updating more information like this. Thanks for sharing. Traffic Lawyer Loudoun VA

Alex said...

Cheap uncontested divorce in virginia
Affordable and efficient uncontested divorce in Virginia. Our experienced team offers cost-effective solutions, guiding you through the process smoothly. Achieve a swift and budget-friendly divorce with our expert assistance.

cicilla said...

Our Fairfax Criminal Lawyer combines legal expertise with a steadfast dedication to protecting clients' rights and interests, all while having a thorough understanding of the nuances of Virginia's legal system. In the ever-changing field of criminal law, where every case is different, our champion takes great pride in developing tactical and tailored defence strategies.Fairfax Virginia Criminal Lawyer

Elijah said...

Your writing always manages to capture my interest from the very beginning. Looking forward to reading more from you. Keep update. bankrupty lawyers near me

Stephen John said...

Reckless driving attorneys employ sound legal strategies to dispute reckless driving charges, whether by challenging the validity of the traffic stop, challenging the evidence presented by the prosecutor, or negotiating with the prosecution to obtain a more favorable outcome for the client. Abogado conducción imprudente Botetourt VA

Wessley said...

The date command is also useful for performing date arithmetic, such as calculating future or past dates, and is widely used in scripts for logging, automation, and time-stamping events. divorcio sin oposición virginia en línea