Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Feb 11, 2011 in Blog, Programming | 9 comments

Date and 24 hour time batch file variables


I’ve lately been using batch files more and more to do backups and other things.
One of the things that’s given me troubles is using dates and times to add to folders or file names to help with organizing.
What I wanted to end up with is something like 20110211_185530.zip.  The first part is the year then month then date followed by an underscore the hour then minutes then seconds.

For the impatient out there here is the final code I use to get this date and time stamp:
I’ll explain why it looks more complicated than you (and I) might have thought it should be afterwards.

set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
echo %dateseed%

The %Time% variable in a batch file will give you a time like 18:55:30.32
The  %Date% variable will give you  “Fri 02/11/2010″.

Since I’m using this to create file and folder names you can’t have slashes and colons etc. mixed in there.
So, being a VB programmer I started out thinking I could use something like the mid function to take the date and time apart and then put it back together in the string I wanted.

I found that you can take just a part of the %date% variable by doing the following: %date~10,4%
This takes the %date% variable starts at the first character after the 10th and grabs 4 characters.
Cool, so you end up %date~10,4% = 2011 (unless you go back in time to last year or forward to next year in which it will be something else and if you can do that then you probably aren’t reading this.)
You can add a negative sign before the first number and it will go from right to left instead of left to right when setting character position.

So with %date:~10,4%%date:~4,2%%date:~7,2% = 20110211 (because today is 02/11/2011).
Take the time and you get %time:~-11,2%%time:~3,2%%time:~6,2% = 191551 (because it’s 7:15 pm at 51 seconds)

Based on these then I came up with:
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~-11,2%%time:~3,2%%time:~6,2%
echo %dateseed%
This returns: 20110211_191655 and was exactly what I wanted.  So, why is the final code I gave you above so much more complicated?  I started using the above code and had problems when it went to make a folder and the time was like 2 am.
The problem is the hour part of the %time% variable doesn’t return a two digit hour like at 2 am.  The %time~-11,2% would return a 2 with a space before it breaking the whole thing.

So, the first lines in the above code compensates for this by taking the hour and adding 100 to it and then pulling out the last 2 characters of what remains.  So at 2 am you end up with 102, take the last two characters and you get 02.  This makes for a more reliable method of making folders and file names using the %date% and %time% variables.

There might be a much better, shorter way to accomplish this but I couldn’t find it so here’s the final code:
(until someone smarter than me tells me how silly I am for doing it this way and gives me something better)

set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
Set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
echo %dateseed%

9 Comments

  1. this is the best way to get date and time in a batch fle. I haven’t seen a more elegant and compact way of doing this.

    however i did havea one small question. Is there any way, the calculation for hour [hh] can be shortened further?

  2. This is a little late, but I came across your blog entry when searching for a solution to this exact same problem, so I wanted to say thanks!

  3. set hh=%time:~0,2%
    set hh=%hh: =0%
    set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
    echo %dateseed%

  4. Thanks much appreciated an elegant solution saved me hours … thank you

  5. thank you so much for sharing – there are so many other google results to get close, but no cigar… it appears to me the /a +100 approach to making it single digit time friendly is perfect… THANK YOU!

  6. A little script I finally came across while doing my head in looking for an answer. You were 5th on the list of newly opened but un-read pages!!

    FOR /F “TOKENS=1 DELIMS=:” %%A IN (‘TIME/T’) DO SET HH=%%A

    “%date:~-2,4%%date:~-7,2%%date:~-10,2%-%HH%;%time:~3,2% exe files.txt”

    Generates a file named:
    130618-05;58 exe files.txt
    yymmdd-hh;mm exe files.txt

    Got to admit sticking that extra ‘0’ in took some finding!!

  7. Thank you! You saved me a lot of time.

  8. What about this?
    (1) Replace the ‘:’ in the system time with nothing.
    (2) Replace the ‘.’ in the system time with nothing.

    (1) set timetemp=%time::=%
    (2) set timeStamp=%timetemp:.=%

    RESULT: 12223423

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>