Grabbing System Date in a Batch File

Posted On 2012-05-04 by dwirch
Tags: Windows Commandline Scripting Tutorial Windows
Views: 991

Batch programming can be a powerful thing. Basically, anything you can run at a command prompt can be run in a batch file. Coupled with task scheduler, process automation nirvana can be achieved.

However, one thing that is conspicuously is the presence of a date or time variable. For example, if you wanted to date or time encode a filename or create a log entry, you have no way to do that in native batch. But there is a way!

There is a DATE command is available though. With a simple bit of trickery, we can grab that info and store it in a variable, then use it however we want within our script.

The actual code is posted at the bottom of this article, but I want to give a basic flow/explanation of what is going on with it first. Because knowledge is power, that's why. And not everyone is a seasoned IT professional.

  • First, we need to decide if the DATE command is available. Some administrators may limit this command for users. Not sure why, but it happens.
    • First we echo the date and look for the string mm.
    • If the string doesn't exist, the command must not be available, so exit the script.
    • Otherwise, continue to grab the date info.
  • Assuming the date is available, we'll run the DATE /T command. and step through it to find certain delimiters, namely the slash (/)
  • For each delimiter we find the necessary value of the year, month or day, and store it in a variable (YYYY, MM, or DD)
  • After all pieces are found, they are combined into a single variable, DateStamp

Pretty straightfoward, eh? At the bottom of the code, there is a small section on how to use the variable in some file operations, such as creating a directory or copying a file.

Anyway, here is the code, watch out for line wrap. Hope this helps!

.echo off
echo *** Finding DateStamp YYYYMMDD

REM ***
REM *** Echo date to null. If the date command is not available, exit the script
REM ***

echo. | date | FIND "(mm" > NUL
If errorlevel 1,(call :Parsedate DD MM) Else,(call :Parsedate MM DD)
goto :ScriptDone

:Parsedate ----------------------------------------------------------

REM ***
REM *** The date is available. Parse the output of DATE /T
REM ***

For /F "tokens=1-4 delims=/.- " %%A in ('date /T') do if %%D!==! (set %1=%%A&set %2=%%B&set YYYY=%%C) else (set DOW=%%A&set %1=%%B&set %2=%%C&set YYYY=%%D)
(Set DateStamp=%YYYY%%MM%%DD%)

REM ***
REM *** Example usage of DateStamp variable
REM ***

echo *** Datestamp:%DateStamp%

REM ***
REM *** Sample usage of the variable
REM ***

REM *** first, create a directory with the datestamp pre-pended to the directory name

mkdir k:\backup\%DateStamp%-webroot

REM *** copy a file with the date in the filename

copy c:\myfile.txt k:\backup\%DateStamp%_myfile.txt


About the Author

dwirch has posted a total of 173 articles.

You can find more information from dwirch by visiting

Comments On This Post

By: dwirch
Date: 2013-08-25

Anyone notice the bug in the code above?

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers

When setting up a certificate authority ...
dwirch posted on July 13, 2017 at about 9:07 in General