Link to home
Create AccountLog in
Avatar of abgtemp
abgtempFlag for United States of America

asked on

Format date output in MS-DOS

Is there a simple way to format the date command in MS-DOS to display the date in a format like (July 21, 2008)?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image


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%
Avatar of AmazingTech
AmazingTech

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

ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.

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).
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
Avatar of abgtemp

ASKER

Thanks guys!! Leew's solution works best for me since it is a batch only script.