Using data and time in batch files

If you need to use the current date and time within batch files, I find this code very useful:


For instance, say you’re moving or copying a file somewhere and you want to rename it with today’s date and time, you could set a filename variable like so:

set filename=%date:~-4,4%-%date:~-7,2%-%date:~-10,2%#%time::=-%

This would output a string with the format (ms being micro-seconds):


To add the computer name to the string use:



  1. Love the blog

    • Adam Rush says

      Thanks Carl. I’m a newbie, but it feels good to get this stuff out of my brain, and that others may find it useful.

  2. I as well as my buddies have been studying the good advice from the website and so suddenly came up with a horrible feeling I never expressed respect to the blog owner for them. My guys were for that reason glad to read them and have in effect really been using these things. Thanks for genuinely indeed accommodating and for selecting this form of decent things most people are really desirous to understand about. My sincere apologies for not expressing gratitude to you sooner.

  3. Great tip, Adam. Did you figure this out on your own or was it passed on? EIther way, thanks for sharing!!

    • I’ve used several snippets like this over the years, but cannot take credit for them. I no doubt “picked the bones of the internet” for the code above. However, my Grandad was one of the creators of DOS itself, so maybe it was passed down genetically. That was a lie 🙂

  4. Way cool! Some very valid points! I appreciate you penning this write-up and
    also the rest of the website is really good.