Computing.Net > Forums > Disk Operating System > New method to get system date

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

New method to get system date

Reply to Message Icon

Original Message
Name: Secret_Doom
Date: April 2, 2003 at 18:28:47 Pacific
Subject: New method to get system date
OS: DOS / Win9x
CPU/Ram: -
Comment:

Hello. To the most questioned FAQ about batch scripting from this forum, how to get system's current date into a variable without the separators, I've been giving this solution for DOS and Win9x:

:: Get current date into variable without separators
@echo off
echo.EXIT|%COMSPEC%/K PROMPT SET %%1=$D$_|FIND " "> %TEMP%.\T1.BAT
type nul> %TEMP%.\T2.DAT
for %%? in (e10D''3B e110''3B w q) do echo %%?>> %TEMP%.\T2.DAT
DEBUG %TEMP%.\T1.BAT < %TEMP%.\T2.DAT > nul
call %TEMP%.\T1.BAT DATE
:: Next line defines date format. The values generally are:
:: %%1=Week day %%2=Month %%3=Date %%4=Year (4 digits)
:: Those could change, depending mainly on the COUNTRY
:: command (from config.sys).
echo SET DATE=%%2%%3%%4> %TEMP%.\T1.BAT
call %TEMP%.\T1.BAT %DATE%
for %%? in (T1.BAT T2.DAT) do del %TEMP%.\%%?
echo DATE=%DATE%

Although that method works fine on most cases, it depends on the country settings from config.sys. The known date formats for DOS are:

MM/DD/YYYY
DD/MM/YYYY
YYYY/MM/DD

The separators may change ("/" "-" or ".") but the formats are those. The script only works as intended with the first format, which is used on US (country code 001). If the date format is the second one, used on several countries such as UK and Brazil, the month and date tokens appear swapped on the script, and if the date format is the third one, the variables get broken, all screwed.

So, though it works on many situations, it's not 100% sure to work as intended, so it's not that good.

I've done some research and found another method, developed by Laura Fairhead, which uses a little assembler script to get the date and solves all the problem. Here's it:

:: Get system's current date into multiple variables
@echo off
if "%1"=="GoTo" goto %2
echo e180 BF 07 01 B4 04 CD 1A E8 02 00 89 CA E8 00 00> %temp%.\t1.src
echo e18F 86 D6 88 D0 D4 10 0D 30 30 86 C4 AB 47 C3>> %temp%.\t1.src
for %%? in (rip 180 g w q) do echo %%?>> %temp%.\t1.src
echo SET %%1=XX XX XX XX> %temp%.\t2.bat
DEBUG %temp%.\t2.bat < %temp%.\t1.src > nul
call %temp%.\t2.bat _
%0 GoTo parse %_% MM DD YC YY
:parse
set %5=%1
shift
if not "%5"=="" goto parse
for %%? in (t1.src t2.bat) do del %temp%.\%%?
set _=

The variables are:

DD - date
MM - month
YC - year century (first two digits from year)
YY - year (last two digits from year)

That method is already updated on my FAQ.

I'm sorry for distributing that faulty method on the past months (maybe years). However, I did warn users of its limitations on the source of the script itself (but I didn't consider the third date format, which brokes the variables).

That's all. BTW, I've just updated 10 FAQs from my page, and more improovements are coming soon.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_________________________________________________________________________


Report Offensive Message For Removal


Response Number 1
Name: Secret_Doom
Date: April 2, 2003 at 18:55:31 Pacific
Reply: (edit)

Additional notes:

1. According to Laura Fairhead, the author of the method mentioned, it is compatible with DOS 5+ (though only tested on DOS 7). I've tested it on WinXP and it works as well (though simpler methods are avaliable for that OS).

2. The script posted is an addaptation from Laura Fairhead's original article. You may find that article here.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal

Response Number 2
Name: Secret_Doom
Date: April 2, 2003 at 19:00:13 Pacific
Reply: (edit)

Oh, and that method does not require any 3rd party applications. It is also not dependent on any local settings, including the country setting (as mentioned before) and the language of the command interpreter.

That means it ought to work on any standard DOS (5+) environment, without needing any auxiliar files.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal

Response Number 3
Name: bitbyte
Date: April 3, 2003 at 08:57:41 Pacific
Reply: (edit)


CONGRATULATIONS,
nice idea to create a temp file and read the time and date from it

that makes my small datetime program not longer nessesary :))

respect to secret doom


Report Offensive Follow Up For Removal

Response Number 4
Name: bitbyte
Date: April 3, 2003 at 09:23:36 Pacific
Reply: (edit)

oh sorry i am lame, forget the line with the file, i have drunken too much beer :)


Report Offensive Follow Up For Removal

Response Number 5
Name: Secret_Doom
Date: April 3, 2003 at 09:36:41 Pacific
Reply: (edit)

Ok, no problem!

Thanks for the comments. However its not my achievement, as I said I'm not the developer.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home








Do you have a Desktop Computer anymore?

No
Yes, but only at work
Yes, but its rarely used
Yes, and its a workhorse


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History




Data Recovery Software