Avatar of abgtemp
abgtemp
Flag 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)?
Microsoft DOS

Avatar of undefined
Last Comment
abgtemp

8/22/2022 - Mon
Steve Knight


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%
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
Lee W, MVP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Knight

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.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Lee W, MVP

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 Knight

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
abgtemp

ASKER
Thanks guys!! Leew's solution works best for me since it is a batch only script.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.