We help IT Professionals succeed at work.

How do you get the Date and Time into the format CCYYMMDDHHMMSS?

Richard Quadling
Medium Priority
Last Modified: 2012-06-21
Very simply question.

I am using an Win32Console Zip program (ZipMagic's) to create a regular backup of a development directory.

I want to create a zip file (I can do that bit no problems) with the name containing the date and time. This would allow me to not have to move the file manually or rename it.

By using this method, I can schedule the zip every 5 minutes and have a load of backups for the few hours I work on the project. If I want to go back, I can choose when to go back to.

I can also take a copy manually by running the schedule by hand.

How can I get todays date and time into a batch file.

The batch file I want to run is ...

zipmd -a -e4 -nr -r DevCCYYMMDDhhmmss.zip dev

The seconds are not too important, so DevCCYYYMMDDhhmm.zip will do.

I can get the date and time as environment variables, but they have the / and : in them.


@echo off
echo | more | time | Find "Current" > 01010.bat
call 01010
echo | more | date | Find "Current" > 01010.bat
call 01010


IF "%1"=="time" SET TODAYS_%1=%3
IF "%1"=="date" SET TODAYS_%1=%4

Results in ...


But I don't know how to remove the / and the : from them.

Any ideas?


Richard Quadling.
Watch Question

What DOS version are you using?
It is possible with NT, but older DOSs are not very useful with different date formats. If you are happy with names like 01-10-2001 then you should take a look at rin1010' answer in http://www.experts-exchange.com/jsp/qShow.jsp?ta=msdos&qid=20164268



Hi Richard... I have some scripts to parse strings and will paste one here
that I think will do what you need... Like Werner I'm wondering about your os version
and also what language... i.e., is the country setting a European format?
You mention the date separator is a foreslash (virgule, instead of a dash)
although these scripts can work with both. You indicate using the More and Find apps
so I presume you don't mind calling externals... I'll paste a script using Choice
(which is easier to maintain) and you can try it if you want...
I need to rewrite it to strip the seconds and produce the format you specified
but I'll post it here in a little while after testing it with the slashes...

Hi.  It seems to me from your sample batch that you're using NT.

If this is the case and you're using an English format, this should do what you've described:

@echo off

echo.|time >holdstr
for /f "eol=E tokens=1,2,3*" %%a IN (holdstr) do echo %%a%%b%%c%%d  >holdstr
for /f "tokens=1,2* delims= " %%a IN (holdstr) do echo %%b  >holdstr
for /f "tokens=1,2* delims=." %%a IN (holdstr) do echo %%a  >holdstr
for /f "tokens=1,2,3 delims=:" %%a IN (holdstr) do echo %%a%%b%%c  >holdstr
for /f "tokens=1,2* delims= " %%a IN (holdstr) do set TODAYS_TIME=%%a%%b

date /t >holdstr
for /f "tokens=1,2* delims= " %%a IN (holdstr) do echo %%b  >holdstr
for /f "tokens=1,2,3* delims=/" %%a IN (holdstr) do set TODAYS_DATE=%%a%%b%%c

del holdstr

echo Todays Date is: %TODAYS_DATE%
echo The Time is: %TODAYS_TIME%


Here's a script that does most of it for you. Maybe you can use it "as is"
or debug it for your os and version. It gets the date and time into variables
similar to the methods you posted, then parses the strings
to remove the components mentioned.

As previously noted, this requires Choice.com ... I was testing another script
using only internal commands which works ok under DOS 7 and below.
The FOR command handles the slash delimiter differently under DOS 7
as well as certain other os/versions so you may need to alter the way I did that part.
It wouldn't be a problem on a system having the date formatted with a 'dash' separator,
but may need editing for your particular system. This portion of the script having the line:

for %%x in (/ - : [) do if "%1"=="%%x" shift

...is where I put the separators to be removed. In the above I included
the foreslash ( / ), dash ( - ) and colon ( : ) as characters to parse and eliminate.
The 'bracket' ( [ ) character needs to be there, but the other three characters
can be removed or others added. I used the For loop while I tested
parsing various characters (like the dash, which is the date separator
on the system I was testing on) because it was more convenient.
(If you include a character that doesn't exist in the string, it's just ignored.)
That For loop does the same thing as the following four lines would do:

if "%1"=="/" shift
if "%1"=="-" shift
if "%1"==":" shift
if "%1"=="[" shift

So if the For loop doesn't work for you to remove the slash (dependent on your os/ver)
then you can remove that For loop line and just add lines using the IF command
to test for the characters you need to remove.

For example, if you need to remove only the date string's slash separator
and the time string's colon separator, you can eliminate that For loop
and replace it with the IF command to test for the characters you're concerned with, like:

if "%1"=="/" shift
if "%1"==":" shift
if "%1"=="[" shift

Again, note that the bracket in the third line above is part of the script and must be used
(and placed after the line having the characters to be removed) if you don't use that For loop.

The portion that goes like this:

if "%col%"=="2" goto end
if "%col%"=="1" set col=2
if "%1"==":" set col=1

...is just a way I used to stop the parsing to leave off the seconds as you mentioned.

The results are processed to a string and assigned to the %vardt% variable.
(You can rename the variables.) You can then use that variable as needed.
The end of the script echoes the contents of the %vardt% variable to the screen
before removing and clearing the temporary files and variables.

So if the Date is 10-01-2001 and the Time is 12:38:14.65p
the script will return a string of:


Using the examples you posted, having a Date in the format of 01/10/2001
and a Time of 11:25:41.00, the script will return a string of:


Then just append the %vardt% variable to the Dev portion in your zip command, like:

zipmd -a -e4 -nr -r Dev%vardt%.zip dev

Hopefully this works for you and/or you can use some of it
to get the string formatted as necessary...
Post back if you want more about it.
Here's the script:

@echo off
for %%x in (%dvar% %tvar%) do if not "%%x"=="" goto got
echo set dvar=%%4>current.bat
call ~rin1010.bat
echo set tvar=%%3>current.bat
call ~rin1010.bat
%0 %dvar%%tvar%
if "%rin%"=="1010" goto quad
set rin=1010
echo ;|choice/c;%1; %0;>~rin1010.bat
if "%col%"=="2" goto done
if "%col%"=="1" set col=2
if "%1"==":" set col=1
for %%x in (/ - : [) do if "%1"=="%%x" shift
if "%1"=="]?" goto done
set vardt=%vardt%%1
goto quad
echo %vardt%
for %%x in (~rin1010 current) do if exist %%x.bat del %%x.bat
set vardt=
set dvar=
set tvar=
set col=
set rin=

Richard QuadlingSenior Software Developer


I'm using Win98SE (DOS 7 I suppose).

The line ...

echo ;|choice /c;%1; %0;>~rin1010.bat

gives me a problem.

I get the following on screen.

Bad command or file name
CHOICE: only one prompt string allowed. Expected Form:
    CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

Adding REM to the @ECHO OFF ...

<11:12:04 Tue 02/10/2001 C:\Source\Utils>for %x in (02/10/2001 11:12:04.04) do if not "%x"=="" goto got

<11:12:04 Tue 02/10/2001 C:\Source\Utils>if not "02"=="" goto got

<11:12:04 Tue 02/10/2001 C:\Source\Utils>if not "/10"=="" goto got

<11:12:04 Tue 02/10/2001 C:\Source\Utils>if not "/2001"=="" goto got

<11:12:04 Tue 02/10/2001 C:\Source\Utils>if not "11:12:04.04"=="" goto got

<11:12:04 Tue 02/10/2001 C:\Source\Utils>if ""=="1010" goto quad

<11:12:04 Tue 02/10/2001 C:\Source\Utils>set rin=1010

<11:12:04 Tue 02/10/2001 C:\Source\Utils>echo ;|choice/c;02/10/200111:12:04.04; d;>~rin1010.bat

<11:12:04 Tue 02/10/2001 C:\Source\Utils>~rin1010.bat

<11:12:04 Tue 02/10/2001 C:\Source\Utils>Invalid switch on command line. Expected form:
Bad command or file name

<11:12:04 Tue 02/10/2001 C:\Source\Utils>    CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
CHOICE: only one prompt string allowed. Expected Form:
    CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

<11:12:04 Tue 02/10/2001 C:\Source\Utils>
<11:12:04 Tue 02/10/2001 C:\Source\Utils>

<11:12:04 Tue 02/10/2001 C:\Source\Utils>

I am not too sure what the ; does in the problem line.

Richard QuadlingSenior Software Developer


I think the ; after /c should be : as this is the seperator for /c, but the : in the time breaks that.

I have a program called repl, which works ...

REPL 1.0   Multi search/replace utility, written by John Bridges


 Format of SRLIST is one string per line in file, 1st line is 1st search
string, 2nd line is 1st replace string, 3rd line is 2nd search string, 4th
line is 2nd replace string.  Blank lines for search string are skipped, so
you can have leading or trailing blank lines in your file that contains the
list of strings to search/replace.
 In either the search or replace string you can use the '\' character to get
unusual characters,
        "\r" = chr$(13)  carrage return
        "\n" = chr$(10)  line feed
        "\b" = chr$(8)   backspace
        "\t" = chr$(9)   tab
        "\#" = chr$(#)   # is a decimal number from 0 to 255

 The names of files to be affected can include wildcards, ie:


which I am going to try and use.

I'll post the results shortly.

If I get it working, I'll split the points as both of your ideas are close to what I want, but I'm not quite able to get them to work for me.


Richard QuadlingSenior Software Developer


Here is what I ended up with ...

IF NOT "%1"=="" GOTO Got

REM Reset variables


REM Create and call CURRENT to get date and time

REM Build seperator file

REM Create another batch file to have the bad characters replaced.

REM Now run the new batch file which will do the backup!

REM Tidy up

REM Finished

REM The whole reason for this program.

REM Remove the backup files.

REM Create datetimestamped backup.
C:\PROGRA~1\ONTRACK\ZIPMAGIC\ZMZIP -a -e4 -nr -r C:\BORLAN~1\POSTIL~1\Dev%3%2%1%4%5.zip C:\BORLAN~1\POSTIL~1\DEVELO~1\*.*


This batch file is run every 15 minutes and I end up with a nice zip file with the name containing the date and time and in ccyyddmmhhmm sequence.

I'm happy.

If anyone can tidy up the code or suggest a better way of doing this, I would be quite happy to see that!


Richard Quadling.

Richard QuadlingSenior Software Developer


Not exact answer, but gave me enough to try another idea.




So what's with the b, Richard? I'd found the problem and was composing some notes
about it to help you further. As a registered expert I'd think you'd do better than that
or furnish some feedback allowing the expert helping you to provide more if needed
before grading. Although I offered a script customized for your specific goal
and stated that it may need debugging for your system settings and asked for
feedback regarding your os' language, you received some complex and useful info
concerning what you called a "Very simply question."
I ask that you request a grade change or points refund from Community Support
or perhaps explain why you consider the extensive help and info offered
worth only a B... Just the fact that someone spends that much time
trying to help should always be worth an A regardless of the outcome.
Hope you understand. Here's the info I'd already composed
in an attempt to continue helping you...
It's offered in the same spirit and is again hopefully helpful...


Sorry it doesn't work for you, Richard. The problem lies in your system's use of the
slash character as a date separator. At that point the script calls Choice
to generate a comma delimited list of "choices" to pass as parameters...
When the script iterates, it parses and retains the desired info from that list.

So using your example output above (in the line causing the error)
if you manually run Choice and pass it those parameters,
using a 'dash' as the date separator on a system using the format MMDDYY
and the hundredths of seconds, Choice generates a list like:


Note the P designating PM but yours shows a "d" ... The semicolons aren't a concern,
but when your system passes Choice the date having the slashes, like:

choice/c;02/10/200111:12:04.04; d;

...Choice interprets those foreslashes as command line switches
and generates the "Invalid switch on command line" error message.

There are other batch script methods to deal with the slash character
(which has always caused problems when writing scripts for multiple languages.)
If you can't or don't want to change the format, you'll probably encounter
this problem again sometime, as the slash can usually be used as a switch with apps
and isn't a valid character for file names under msdos and Windows.

Hopefully some of the ideas in that script will be useful to you in other projects.
Meanwhile, post back if you want a small utility that will work in any language
and format the date and time as you specified or a batch script that will do it
using your language settings and doesn't rely on externals like choice.com, etc...
Richard QuadlingSenior Software Developer


For me, a grade A means that the answer was provided, or an example that can be adapted without significant re-writing.

I feel that from your information, I would be unable to achieve a working result. Whether this is because my system is UK based and shows dates with a / or because I'm a bit dim on some of the "clever" methods you use is not really the issue. I DID try to adapt the script, but I was not too sure how to get around the problems I faced with it.

It did provide me with a closed route. It is sometimes better to know something CANNOT be done than to know it CAN be done. Because of this, I pulled in another program and I end up with a working script.

I feel that the Grade B is justified as you DID provide information that helped but was not enough for me to complete the task.


Richard Quadling.
I know this is a very old topic, but I came across it by searching, and no doubt someone else will too.  So here is my addition.  Please note that my advice is grounded on the English version of Windows XP.

I found the other answers mind boggling.  Here is a one- or two-line way to remove the slashes (/) from the date, and the colons (:) and periods (.) from the time:

for /f "tokens=1,2 delims=:." %%a in ("%time%") do set ctime=%%a%%b

for /f "tokens=1,2 delims=MonTueWedThuFriSatSun/ " %%a in ("%date%") do set cdate=%%a%%b

What happens is that the FOR statement causes the DATE and TIME strings to be parsed, with the specified delimeters ("delims") marking where individual tokens begin and end.  The delimiters themselves are not taken as tokens.  With the above code...

This %date% value:

Thu 01/23/2003

Becomes this %moddate% value:


...and this %time% value:


Becomes this %modtime% value:


This code has flexibility, as well.  If, for example, you want the day of the week present in %moddate%, you simply need to remove the day-of-week strings from the "delims" portion of the FOR statement, and you also need to add a token number specification and variable.  For example:

This statement:

for /f "tokens=1,2,3 delims=/ " %%a in ("%date%") do set cdate=%%a%%b%%c

...would change a %date% value of:

Thu 01/23/2003

...to a %moddate% value of:


Above, I told the FOR statement to save 3 tokens (1,2,3), and gave the output somewhere to go by adding %%c on the end.  Removing the "MonTueWedThuFriSatSun" string resulted in the day of week not being stripped.

If you wanted the four-digit year left in, you'd keep the FOR statement as above, except that you'd keep in the day-of-week delimiter string:

for /f "tokens=1,2,3 delims=MonTueWedThuFriSatSun/ " %%a in ("%date%") do set cdate=%%a%%b%%c

The same kind of flexibility applies to the %time% as well, of course.  How would you add seconds to the %modtime% output?  Easy; you just add a token number specification and variable:

for /f "tokens=1,2,3 delims=:." %%a in ("%time%") do set ctime=%%a%%b%%c

Hopefully that is enough of an explanation that any reader can take the ball and run with it!  Just remember that you may have to use different delimiters than I've shown here.  For example, for all I know, different language versions of Windows XP use commas (,) instead of periods (.) in the %time%.  This is just an example, but you can see what I mean.
Richard QuadlingSenior Software Developer


Thanks for that. A LONG time ago! Ha!