We help IT Professionals succeed at work.

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

Richard Quadling
on
Medium Priority
2,456 Views
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.

DOIT.BAT

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

and CURRENT.BAT

@ECHO OFF
IF "%1"=="time" SET TODAYS_%1=%3
IF "%1"=="date" SET TODAYS_%1=%4

Results in ...

TODAYS_TIME=11:25:41.00
TODAYS_DATE=01/10/2001

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

Any ideas?

Regards,

Richard Quadling.
Comment
Watch Question

Commented:
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

======
Werner

Commented:

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...
 

Commented:
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
cls

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%
Commented:

Richard,

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:

100120011238

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:

011020011125

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
cls
for %%x in (%dvar% %tvar%) do if not "%%x"=="" goto got
echo.|date>~rin1010.bat
echo set dvar=%%4>current.bat
call ~rin1010.bat
echo.|time>~rin1010.bat
echo set tvar=%%3>current.bat
call ~rin1010.bat
%0 %dvar%%tvar%
:got
if "%rin%"=="1010" goto quad
set rin=1010
echo ;|choice/c;%1; %0;>~rin1010.bat
~rin1010.bat
:quad
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
shift
goto quad
:done
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
CERTIFIED EXPERT

Author

Commented:
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
CERTIFIED EXPERT

Author

Commented:
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

REPL SRLIST FNAME1 [FNAME2 FNAME3 FNAME4.....]

 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:

        REPL MYLIST TRY*.C

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.

Thanks.

Richard.
Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
Here is what I ended up with ...

@ECHO OFF
IF NOT "%1"=="" GOTO Got

:Get
REM Reset variables
SET TODAYS_DATE=
SET TODAYS_TIME=

REM Build CURRENT.BAT
ECHO @ECHO OFF > CURRENT.BAT
ECHO IF "%%1"=="date" SET TODAYS_DATE=%%4 >> CURRENT.BAT
ECHO IF "%%1"=="time" SET TODAYS_TIME=%%3 >> CURRENT.BAT

REM Create and call CURRENT to get date and time
ECHO | MORE | TIME | FIND "Current" > DOIT2.BAT
CALL DOIT2
ECHO | MORE | DATE | FIND "Current" > DOIT2.BAT
CALL DOIT2

REM Build seperator file
ECHO />SEPS
ECHO. >>SEPS
ECHO :>>SEPS
ECHO. >>SEPS
ECHO .>>SEPS
ECHO. >>SEPS

REM Create another batch file to have the bad characters replaced.
ECHO CALL BACKUPDEV %TODAYS_DATE% %TODAYS_TIME% > DOIT2.BAT
REPL SEPS DOIT2.BAT > NUL

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

REM Tidy up
FOR %%F IN (CURRENT.BAT DOIT2.BAT SEPS) DO IF EXIST %%F DEL %%F

REM Finished
GOTO Done

:Got
REM The whole reason for this program.

REM Remove the backup files.
DELTREE /Y C:\BORLAN~1\POSTIL~1\DEVELO~1\*.~*

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\*.*

:Done



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!

Regards,

Richard Quadling.

Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Author

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

Thanks.

Richard.

Commented:

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:

;[;,1,0,-,0,2,-,2,0,0,1,1,1,:,1,2,:,0,4,.,8,3,P,;]?;

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
CERTIFIED EXPERT

Author

Commented:
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.

Regards,

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:

0123

...and this %time% value:

23:42:15.07

Becomes this %modtime% value:

2342

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:

Thu0123

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
CERTIFIED EXPERT

Author

Commented:
Thanks for that. A LONG time ago! Ha!