Apr
02

Getting the current date and time in batch files

By Q

I still do the occasional batch file solution for tasks, and sometimes I need to get the current date and time on the system for processing. Since I just had to research this again for another little project, I decided it was high time to blog it.

Batch files can use the system variables %DATE% and %TIME% to get the current date and time of the system. These variables return data in the following format:

%DATE% gives DOW MM/DD/YYYY (may vary depending on you location – you can run echo %DATE% at a command prompt to see what values you get). So for today, as I’m writing this post, the output is:

Sun 04/02/2006

%TIME% gives HH:MM:SS.hh where HH is hour (in 24-hour format), MM is minute, SS is seconds, and hh is hundredths of seconds. Who needs hundredths of seconds? Don’t know for sure, but it’s there if you want.

This is all well and good, but what if I only need to know the current minute? Easy! cmd.exe gives you a way to pull substrings out of variables right in the batch file. If I only want to know what the current minute is, I can refernce that as %TIME:~3,2%.

Side note: a quick explanation of this parsing. The “:~” characters tell the batch file to return a subset of data from the variable. The number that immediately follows :~ is the character position to start with, and the first character in the string is always 0. So %TIME:~0% is functionally equivalent to %TIME%. Optionaly after the number can be a comma followed by another number, indicating how many characters to include. So the reference %TIME:~3,2% tells the batch file to return two characters starting at position 3 from the current time. In other words, the two characters that represent the current minute.

In addition, if you want the last two digits of the year (for those who still use a 2-digit year value) you can reference %DATE:~-2%. The “-2” indicates the last two charactes in the string, so you don’t have to count the entire string length. If you want the four-digit year, use %DATE:~-4%.

Rather than figure all this out every time, here’s a quick menu of values that I use regularly.

Item Variable
Current hour: %TIME:~0,2%
Current minute: %TIME:~3,2%
Current second: %TIME:~6,2%
Current day of week: %DATE:~0,3%
Current month: %DATE:~4,2%
Current day date: %DATE:~7,2%
Current year (2 digit): %DATE:~-2%
Current year (4 digit): %DATE:~-4%
Categories : How To

4 Comments

1

Things I wish I had known is very apt. Thankyou for saving me from the depths of dispair :-) )

2

nice posting, really helped me a lot

3

This is great, but what if the hour is less than 10? How can I add a zero to the beginning of the hour? I am trying to create a batch file that appends date and time to a filename and when it is before 10 AM, I get a space in the date/time and the program bombs!

Any suggestions would be appreciated.

4

David – you’ll have to use a bit of progamming for that. I’d do something like this:

set myHour=%TIME:~0,1%
if ‘%myHour%’==’ ‘ goto addzero
goto hourfine
:addzero
set myHour=0%TIME:~1,1%
goto end
:hourfine
set myHour=%TIME:~0,2%
:end
echo %myHour%

This checkes the first character of the hour string, and if it’s a space, it creates the variable with a ‘0′ in front and then the single digit following. If the first character is not a space, then it accepts the hour as is.

HTH…

-Q

Leave a Comment