Windows batch files - date/time

Associate
Joined
19 Jun 2003
Posts
1,680
Location
West Yorks, UK
Hi all,

I'm trying to find a reliable way of using dates and times in Windows batch files. I've been using the following, which works on my XP install, on my works Server 2003 SBS install, but not on some other Server 2003 installs - it's like the date is in a completely different format:

Code:
SET NowDate="%date:~6,6%-%date:~3,2%-%date:~0,2%"
SET NowTime="%time:~0,2%-%time:~3,2%"

echo Date: %NowDate% >> c:\test\logs\%NowDate%_%NowTime%.txt
echo Time: %NowTime% >> c:\test\logs\%NowDate%_%NowTime%.txt
echo ------------------ >> c:\test\logs\%NowDate%_%NowTime%.txt

Does anyone know of a reliable way to get the date/time so it will work on all Windows installs, regardless of date format? (ideally, it should be in YYYY-MM-DD HH:MM format, and shouldn't require the use of any extra software to be installed).

Cheers,
Matt
 
Associate
OP
Joined
19 Jun 2003
Posts
1,680
Location
West Yorks, UK
Found the problem. XP seems to treat dates as "04/10/2006", whereas for some reason, Server 2003 outputs them as "Wed 04/10/2006".

After altering the parsing script a little, I have it working. Bit odd that the dates are setup differently though. Anyone know of a way to do it reliably regardless of the Windows version? Grabbing the day/month/year seperately would do the trick instead of having to parse them from a string.

Matt
 
Back
Top Bottom