Rob van der Woude's Scripting Pages

DATE and TIME commands

  1. General
  2. Date and Time using PROMPT
  3. Date and Time using BATCHMAN by Michael Mefford
  4. For ease of use: ERRTIME or REALDATE
  5. Date and Time in NT:
  6. Non-batch solutions
  7. Examples showing how to create directories named after the current date or week number (these examples use the scripts demonstrated in the Non-batch solutions section besides batch commands)
  8. Use Date and Time to prefix strings
  9. GETDATE (NT)
    This post to alt.msdos.batch.nt by Simon Sheppard, based on a previous post by Michael Jerkovic, shows an ingenious way to get today's date in an almost language independent format.
    Why "almost"? Because this batch file will fail when the year is not the last part of the date string, as in YYYY-MM-DD (very well possible), or when no delimiters are used, as in DDMMYYYY (highly improbable).
    Contrary to my own SortDate (3.*) solution which uses REGEDIT, Simon's/Michael's solution only uses internal commands.
  10. Advanced date math in NT, or how to calculate the date several days ago or several days ahead (link to a post on alt.msdos.batch.nt by Phil Robyn)
  11. Frank's Calendar Script demonstrates several interresting batch techniques, like how to determine if this is a leap year (link to a post on alt.msdos.batch.nt by Frank)
  12. Dr. J.R. Stockton's Date and Time section contains a lot of useful information on our calendars and many routines to handle date and time in several languages.
    And it contains (I quote) "possibly the only code that can calculate the date of Gregorian Easter Sunday in the year 0123456789ABC working entirely in base 13".
    Hard to imagine you wouldn't find the date/time related information you're looking for here.
  13. A frequent question is how to rename log files with their date and time.
    If you don't know how to use the examples given here, or if you prefer a Windows version independent tool, have a look at RenTS by Kees Hiemstra
  14. At TimeAndDate.com you'll find world clocks, a timezone calculator, and more date and time related goodies
  15. At TheTimeNow.com you'll find world clocks, a timezone converter, and more date and time related information too, more accessible for sight impaired individuals!
  16. Use my GetDate Date Calculator for interactive date calculations, or my Holidays Calculator to list the dates of several (Christian) holidays for any given year
  17. Use NET \\computer /SET or NET /DOMAIN:domain /SET to synchronize the local computer's time with a remote computer or domain controller.
    Type NET HELP TIME for more time synchronization options.

DATE and TIME originally were only meant to adjust the date and time after each cold boot of the first generations of PC's, since these old PC's were not equipped with batteries to keep the internal clock going.

NT and OS/2 provide ways to show only the current date or time, without asking you to adjust it.
In NT, TIME /T and DATE /T will display only the current time and date, respectively.
In OS/2 DOS sessions, TIME /N and DATE /N will do the same, though preceded by a statement that the current time/date is set to the values displayed.

In real DOS there's only the hard way to display date and time:

@ECHO OFF
VER | TIME > TEMP.BAT
ECHO SET TIME=%%3>CURRENT.BAT
CALL TEMP.BAT
DEL TEMP.BAT
DEL CURRENT.BAT
ECHO It's %TIME% now

This batch file, as shown here, works only if your DOS version will say something like:

Current time is 10:59:47.47 AM

when executing the TIME command. If your DOS version isn't English you have to change the name of CURRENT.BAT and the number of the parameter (%%3)

Let's use a Dutch DOS version as an example.

VER | TIME will display:

Tijd is nu ingesteld op: 10:59:47.47

So now we'll have to use TIJD.BAT instead of CURRENT.BAT, and %%3 must be changed to %%5 because now, instead of:

Current time is 10:59:47.47 AM

TEMP.BAT will contain a line:

Tijd is nu ingesteld op: 10:59:47.47

Now the third line of our batch file should be:

ECHO SET TIME=%%5>TIJD.BAT

and the sixth line:

DEL TIJD.BAT

The above batch file can be made multi-lingual.
The price we pay is more code and more temporary batch files to create and clean up:

@ECHO OFF
REM Temporary batch file common to all DOS versions
VER | TIME > TEMP.BAT
REM Temporary batch file for English DOS version
ECHO SET TIME=%%3>CURRENT.BAT
REM Temporary batch file for Dutch DOS version
ECHO SET TIME=%%5>TIJD.BAT
CALL TEMP.BAT
DEL TEMP.BAT
DEL CURRENT.BAT
DEL TIJD.BAT
ECHO It's %TIME% now
Note: An added complication is the use of 12 hour AM/PM vs. 24 hour notation.
6:00 PM is 18:00 so we would need to add 12 hours.
In most cases that won't be necessary, as you can append the AM or PM to the variable using SET TIME=%%3%%4>CURRENT.BAT

These batch files set the variable TIME to the time they are executed.
The same tricks work for DATE as well, you only need to make some minor adjustments.

Back to the top of this page... ]

 

Date and Time using PROMPT

The PROMPT command has some useful options, like $D and $T, that allow us to store the week day, current date and current time in environment variables, independent of the operating system's language!
The resulting environment variable is language dependent, though.
So I added a routine to remove forward slashes (or any other FOR delimiter) from the DATE variable (which is then stored in the DATE2 variable), allowing the variable to be used in file names.
This technique to remove or replace forward slashes works in all DOS versions up to and including MS-DOS 6.22, PC-DOS 7 and OS/2 Warp 4's DOS box. It will not work in MS-DOS 7.*, which means the %DATE2% variable is useless in MS-DOS 7.*. However, the %DATE% and %TIME% variables are valid in all DOS versions, including MS-DOS 7.*.

💾   Download all DATE/TIME sources

Back to the top of this page... ]

 

BATCHMAN

Check out BatchMan, by Michael Mefford, if you want an easy to use and language independent way to check the system time or date (and much more) in batch files.

Note: The way errorlevels are checked in the following batch files require the use of COMMAND.COM as the command interpreter.
Running these batch files with CMD.EXE as the command interpreter will fail!

💾   Download all DATE/TIME sources

Back to the top of this page... ]

 

ERRTIME

Check out ErrTime, by Phil Money, Advantig Engineering & Design, if you want an easy to use and language independent way to check the system time or date in batch files.
Version 1.4 has just (July 4, 2004) been released to the Public Domain.
This version contains executables for DOS, Windows, OS/2 and Novell Netware.

💾   Download all DATE/TIME sources

Back to the top of this page... ]

 


page last modified: 2022-11-04; loaded in 0.0030 seconds