Rob van der Woude's Scripting Pages

NT Batch files:

Examples using DATE and TIME

Download the ZIPped sources Click the floppy disk or Download the ZIPped sources icons to download the ZIPped sources

Checksums for the ZIPped sources Click the question mark icons to view the MD5 and SHA1 checksums for the ZIPped sources

 

Batch file examples
Click the floppy disk icons to download the ZIPped sources Click the question mark icons to view the MD5 and SHA1 checksums for the ZIPped sources Name Description Remarks
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source AtFuture.bat Schedule a command a few minutes from now in the near future This batch file was a coproduction with Rob Fuller.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source DateAdd.bat Add/subtract a specified number of days to/from a specified date Use REGEDIT.
Based on Fliegel-Van Flandern Julian date conversion algorithms from the Astronomical Almanac, provided by Doctor Fenton on the Math Forum and converted to batch code by Ron Bakowski.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source DateDiff.bat Calculate the difference in days between two dates
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source DateFmt.bat Show the current date in the specified format Inspired by Simon Sheppard's GetDate.bat.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source DatePart.cmd Return the specified part of the current date Uses WMIC.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source DateTime.cmd Return the current date as errorlevel in YYYYMMDD format Written by Justin.
Tested only in Windows XP.
Requires modifications to work on non-English systems.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source Easter.bat Calculate dates for Easter Day, Ascension Day and Pentecost for any year Uses REGEDIT.
Easter Day algorithm found at Simon Kershaw's Keeping the Feast.
Julian date conversions based on Fliegel-Van Flandern algorithms from the Astronomical Almanac, provided by Doctor Fenton on the Math Forum and converted to batch code by Ron Bakowski.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source iDate.bat Read the date format setting from the registry Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source IsDate.bat Tests if the specified date is a valid date  
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source IsDST.bat Tests if the current or specified date/time is in Daylight Saving Time Warning: Changes the system date up to 65 times, which may cause problems for domain members.
Use entirely at your own risk.
VBScript and C# versions are safer to use.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source IsRTCSet.bat Check if the CMOS Real Time Clock is set, and return an errorlevel greater than 0 if not Uses DEBUG to read the RTC status from the CMOS.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source iTime.bat Read the time format setting from the registry Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source LeapYear.bat Check if the specified year is a leap year or not Code to extract current year by Simon Sheppard.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source Now.bat Display the current time in HHmmss format with any delimiter you want Uses DEBUG.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source PMSoon.bat Schedule at relative times instead of absolute times A "clone" of the Windows NT 4 Server Resource Kit's SOON command using NT's native NET Time and AT commands only.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source sDate.bat Read the date separator (delimiter) setting from the registry Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SetDate.bat Store the current day in an environment variable Code is more complex than usual to allow it to be used in any NT version; as of Windows 2000 the %DATE% variable can be used instead.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SetTime.bat Store the current time in an environment variable As of Windows 2000 the %TIME% variable can be used instead.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortDate.bat (2.*) Store sorted date in variable, independent of Control Panel's "Regional Settings" Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortDate.bat (3.*) Uses REGEDIT.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortDate.bat (4.*) Uses code from Simon Sheppard's GetDate.bat to convert the current date to YYYYMMDD format.
This technique is not entirely language independent, though it does not depend on regional settings.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortDate.bat (5.*) Uses WMIC.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortTime.bat (2.*) Store sorted time in variable, independent of Control Panel's "Regional Settings" Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortTime.bat (3.12) Uses REGEDIT.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source SortTime.bat (3.40) Uses REGEDIT and FINDSTR.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source StarDate.bat Convert current date and time to stardate Algorithm found on The Star Trek Gallery.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source StarDateXP.bat Convert current date and time to stardate Algorithm found on The Star Trek Gallery.
Uses WMIC.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source sTime.bat Read the time separator (delimiter) setting from the registry Uses REG.EXE.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source Today.bat Display the current date in DDMMYYYY or YYYYMMDD format, with any delimiter you want Uses DEBUG.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source TodayNow.bat Display the current date and time in YYYYMMDDHHmmss format without any delimiters Uses DEBUG.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source Week.bat Display the current week number. Uses REGEDIT.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source WeekXP.bat Display the current ISO week number. More accurate than the NT version
Uses WMIC.
Click the floppy disk icon to download the ZIPped source Click the question mark icon to view the MD5 and SHA1 checksums for the ZIPped source Yesterday.bat Display yesterday's, today's and tomorrow's date in two formats Uses REGEDIT if REG is not available.

 

Notes: Most of the following NT batch files were created and tested in Windows NT 4 and/or Windows 2000.
Most of them will work or can be adapted to work in Windows XP as well.
However, keep in mind that Microsoft has made some changes in the output of the DATE /T and TIME /T commands, depending on the Regional Settings used.

In the Canadian English version of XP for example, DATE /T does not return the Day Of Week, only MM/DD/YY.
Workaround: FOR %%A IN (%Date%) DO SET Today=%%A

DIR's time stamp uses HH:mm AM or PM in XP, instead of 2000's HH:mma or m.
Workaround: That's a long story, just take a look at SortTime.bat to see how Harry Teufel and yours truly solved this.

And last but not least, REG.EXE 2.0 (Windows 2000 and later), is not backwards compatible! This means that code written for earlier versions (i.e. the one from theWindows NT 4 Resource Kit) has to be modified.
Sometimes you don't need to check for REG.EXE's version, just run the command twice, once for the old REG and once for the new one. This technique is demonstrated in iDate.bat, sDate.bat, iTime.bat and sTime.bat.

page last uploaded: 2015-08-24, 21:07