views:

234

answers:

3

Hello, I'm having trouble generating a timestamp in a windows batch file because I get diferent date formats on different windows versions.

My machine:

>echo %date%
>Tue 11/17/2009

Friends machine:

>echo %date%
>11/17/2009

I guess there has to be some way of getting the date (11/17/2009) from both strings using for /f, I've been trying and googling and can't find the answer.

Maybe there is another way to get a timestamp without using %date%.

Thanks in advance.

A: 

Unfortunately, it can't be done directly, so you need to resort to hacks like this one.

There are lots of VBScript and small external commandline tools available too, which isnt something I'd take a dependency on unless you're already using something of that nature in your overall system.

Personally, I'd be trying to route around it by using PowerShell which neatly sidesteps the issue completely.

Ruben Bartelink
+1  A: 

use vbscript if you want to get independent date time settings

thedate=Now
yr = Year(thedate)
mth = Month(thedate)
dy = Day(thedate)
hr = Hour(thedate)
min = Minute(thedate)
sec = Second(thedate)
WScript.Echo yr&mth&dy&hr&min&sec
ghostdog74
This is really usefull used inside the script by:>cscript date.vbs //Nologo(where date.vbs contains the code you included)
eliocs
+1  A: 

you dont need VB, you can do it with something like this:

echo %date:~-10,2%/%date:~-7,2%/%date:~-4,4%

Source: http://www.intelliadmin.com/index.php/2007/02/create-a-date-and-time-stamp-in-your-batch-files/

Darren