Date and Time in Windows NT 4 and later

The Basics

Note: This page uses the designation "NT" for Windows NT 4 and all later 32-bits Windows versions (2000, XP, Server 2003, Vista, Server 2008).
The designation "NT4" is used for Windows NT 4 only, assuming the latest service pack (SP6a).

In NT, assigning the current time and date to variables is fairly easy to do. The following code "saves" the date:

@ECHO OFF
FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
ECHO It's %Today% today

The "inner" or secondary FOR loop is used to make sure we only get the date, without a day of the week "prefix".

For the time we use similar but simpler code:

@ECHO OFF
FOR /F %%A IN ('TIME/T') DO SET Now=%%A
ECHO It's %Now% now

Note, however, that the actual values are still language dependent.
The order of day and month, leading zeroes or not, forward slashes or dashes for date delimiters, day of the week, AM/PM or 24 hours, they all depend on the computer's or user's regional or international settings.

Though the code described above is valid in Windows 2000 and later, I wouldn't recommend using it unless your code has to, or may have to, work in NT4 too.

For Windows 2000 and later, the built-in Date and Time environment variables can be used:

FOR %%A IN (%Date%) DO SET Today=%%A
SET Now=%Time%
ECHO It's %Today% today
ECHO It's %Now% now

Unfortunately, the same dependencies on regional or international settings still apply.

 

 


page last uploaded: 20 December 2011, 10:40