In a batch file I need to extract a month, day, year from the date command. So I used the following, which essentially parses the Date command to extract its sub strings into a variable:

set Day=%Date:~3,2%
set Mth=%Date:~0,2%
set Yr=%Date:~6,4%

This is all great, but if I deploy this batch file to a machine with a different regional/country settings, it fails because month, day and year are in different locations.

How can I extract month, day and year regardless of the date format?

link|improve this question

1  
Are you absolutely restricted to Batch? Such a thing is much simpler in VBScript/WSH and/or PowerShell ... – Adrien Jul 27 '11 at 23:20
@Adrien, Yes limited to batch - it's part of the VS2008 post-build step. – AngryHacker Jul 28 '11 at 0:06
feedback

4 Answers

up vote 3 down vote accepted

I know it's not exactly what you asked for, but I use the Windows port of the Linux date application from a command inside the batch file and then assign the result to a variable.

I have yet to find a way to get the date reliably using only batch commands.

link|improve this answer
feedback

Here is a batch file that addresses exactly this problem:

http://ss64.com/nt/syntax-getdate.html

The trick is that they look what DATE says in which format you need to input the date:

The current date is: Fri 29/07/2011
Enter the new date: (dd-mm-yy)

Based on this information, they parse the extracted date differently.

link|improve this answer
1  
Ah ha, very neat way of finding the locale's date ordering. ss64 does have a nifty community of cmd.exe users. – Nicholi Jul 28 '11 at 22:06
feedback

Have you tried using NET TIME \\%ComputerName% instead of DATE? I think that NET TIME is supposed to provide the data in a consistent format regardless of locale.

link|improve this answer
Nope, it doesn't. – AngryHacker Jul 28 '11 at 0:06
Interesting, I just tried it, switching between AU and US formats, and net time always output in m/d/yyyy format. @AngryHacker, with what settings did it not work for you? – Hydaral Jul 28 '11 at 1:15
I switched French(Belgian) and it gave me 2011-07-27. The US give 07/27/2011 – AngryHacker Jul 28 '11 at 1:30
feedback

While you are right that VS 2008 outputs a batchfile, you can run pretty much any program you want, including Powershell scripts and other programs.

Edit:

Here are some similar questions:

http://stackoverflow.com/questions/1051845/visual-studio-2008-professional-build-process http://stackoverflow.com/questions/3049369/embed-application-compilation-time-stamp

Originally, I was gonna have this moved to SO. . .

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.