How Can We Help?

Using The Date And/Or Time In Batch Files

You are here:
< Back
Using The Date And/Or Time In Batch Files
Last Modified: 06 May 2004
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*** PLEASE NOTE: Link(s), If Provided, May Be Wrapped ***


The following batch files will place the current Date
and Time into environment variables for use within
other scripts.  In most cases, you will only need a
single line or two for the desired result.

Additionally, Win2K provides native %TIME% and %DATE%
variables, although the script fragments still work...

For more flexibility with Date/Time manipulation in
scripts, consider using another scripting language
such as Kixtart, VBScript or Perl.

Also, see the following useful utility:

• XSET ................... http://xset.tripod.com/


SCRIPTING SAMPLES

Please note that the parsing of date variables in all of
the date-related scripts in this archive is dependent on
the Regional Settings in the Control Panel.  These scripts
were all tested with the default English (US) option.


OPTION #1: *** Requires NT4 or higher ***
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 FOR /F "TOKENS=2-5 DELIMS=/ " %%D IN ('DATE /T') DO SET @TODAY=%%F%%D%%E
 FOR /F "TOKENS=1-3 DELIMS=:. " %%T IN ('ECHO %TIME: =0%') DO SET @NOW=%%T%%U%%V
 SET @FILENAME=MyLogfile.%@TODAY%-%@NOW%.LOG
 ECHO %@FILENAME%


OPTION #2: *** Requires NT4 or higher PLUS Server ResKit ***
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 FOR /F "TOKENS=2-5 DELIMS=/ " %%D IN ('DATE /T') DO SET @TODAY=%%F%%D%%E
 FOR /F "TOKENS=4-6 DELIMS=: " %%T IN ('NOW') DO SET @NOW=%%T%%U%%V
 SET @FILENAME=MyLogfile.%@TODAY%-%@NOW%.LOG
 ECHO %@FILENAME%

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


OTHER SCRIPTS & EXAMPLES

• http://www.pcmag.com/article2/0,4149,1386947,00.asp


PERSONAL NOTES

• If you use the RESKIT, then you can use NOW.EXE to get
  the time in a single pass, plus get the 3-letter month,
  instead of the 2-digit month.

• See the DateTime.BAT file for more variations on the
  above theme.

• Please note that the parsing of date variables in all
  of the date-related scripts in this archive is dependent
  on the Regional Settings in the Control Panel.  These
  scripts were all tested with the default English (US)
  option enabled.


RELATED SCRIPTS (ALSO IN THIS ARCHIVE)

• http://KB.UltraTech-llc.com/Scripts/?File=DateTime.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-a.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-b.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-c.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-d.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-e.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-f.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelEmpty.BAThttp://KB.UltraTech-llc.com/Scripts/?File=ZipFiles-A.BAThttp://KB.UltraTech-llc.com/Scripts/?File=ZipFiles-B.BAT


RELATED TOPICS (ALSO IN THIS ARCHIVE)

• http://KB.UltraTech-llc.com/?File=SPack.TXThttp://KB.UltraTech-llc.com/?File=ResKit.TXThttp://KB.UltraTech-llc.com/?File=Scripting.TXThttp://KB.UltraTech-llc.com/?File=Utils.TXT