We help IT Professionals succeed at work.

Format date output in MS-DOS

abgtemp
abgtemp asked
on
1,901 Views
Last Modified: 2012-06-27
Is there a simple way to format the date command in MS-DOS to display the date in a format like (July 21, 2008)?
Comment
Watch Question

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:

With some hefty code and liklihood of making mistakes across different machines with dates setup differently yes... or... you could use a VB Script. You can call that from a batch file still:

wscript.echo MonthName(Month(date())) & ", " & DAY(date()) & " " & Year(date())

save the above line as getdate.vbs say

Then in a batch file you can do:

cscript //nologo getdate.vbs     to show it.

To get it into a variable use:
@echo off
for /f "tokens=* delims=" %%a in ('cscript //nologo getdate.vbs') do set thedate=%%a
echo %thedate%

You can create the VBS on the fly from a batch file using:

@echo off
if not exist getdate.vbs echo wscript.echo MonthName(Month(date())) & ", " & DAY(date()) & " " & Year(date())>getdate.vbs
for /f "tokens=* delims=" %%a in ('cscript //nologo getdate.vbs') do set thedate=%%a
echo %thedate%
Top Expert 2009

Commented:
DOS is truncating July into 3 characters.

'capture existing shortdate format in sShortDate
for /f "tokens=3" %%a in ('reg query "hkcu\control panel\international" /v sShortDate ^| find /i "sshortdate"') do SET sShortDate=%%a
 
'set shortdate to desired output.
reg add "hkcu\control panel\international" /v sShortDate /t reg_sz /d "MMMM dd, yyyy" /f
 
echo %date%
 
'set shortdate back to original format.
reg add "hkcu\control panel\international" /v sShortDate /t reg_sz /d "%sShortDate%" /f

Open in new window

Technology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Yeah I used to use routines in pure batch for dates like leew's above but all you need is someone to fiddle with date formats or update the OS etc. and they can break.  Fine for a script running in known location on known kit etc. but I tend to 'borrow' the VBScript date routines for anything a bit more fancy.

Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
Technically, you can break down date /t which should have a consistent format if you feel there a good chance the information is used outside of the United States (where the default format conforms to the script I posted).
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Trouble is afaik date/t returns the same as %date% anyway?  In the UK here is reports dd/mm/yyyy

H:\>date
The current date is: 22/07/2008
Enter the new date: (dd-mm-yy)

H:\>date /t
22/07/2008

Then change language to US and

H:\>date /t
Tue 07/22/2008

But as we both agree I think if it works on your environment then go for it, just be aware of possibility og breaking.

Steve

Author

Commented:
Thanks guys!! Leew's solution works best for me since it is a batch only script.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.