Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Date by month-day-year in batch file?  (Read 21958 times)

0 Members and 1 Guest are viewing this topic.

Nathansswell

    Topic Starter


    Rookie

    Thanked: 1
    • Yes
    • I make music!
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 8
Date by month-day-year in batch file?
« on: December 07, 2011, 07:05:27 PM »
I am wondering if it is possible to have a batch file display the Month first, then the day, then the year.

I have this, but it is in the wrong order and I don't understand it. I found it off some forum, but it's not what I want...

Code: [Select]
@echo off
set date=%date:~10,4%%date:~7,2%%date:~4,2%
echo %date%
pause >nul

If this were today, it would echo "20110712."
I would like mine to be "December 07 2011."
If that is not possible then I'd be fine with "12072011"

Also, congrats for the next post in the "Microsoft DOS," you are the 8,888th topic!!!

-Nathansswell

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Date by month-day-year in batch file?
« Reply #1 on: December 07, 2011, 07:27:58 PM »
The date variable is dependent on your Regional settings in control panel.

My date is currently set like this.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date%
Wed 12/07/2011
So the first thing you need to understand is that computers start counting by 0.  So if I want to pull out the day of the week I would need to reference position 0 for a length of 3.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date:~0,3%
Wed
If I want the year I could do it two different ways.  I could reference the 11 position by using the number 10 for a length of 4.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date:~10,4%
2011
Or I could reference from the back side by using a negative number first.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date:~-4,4%
2011

Does that help you out.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Date by month-day-year in batch file?
« Reply #2 on: December 07, 2011, 07:40:25 PM »
I customized my SHORT DATE format in my Regional settings and there doesn't seem to be be a way to completely spell out the Month.  It will only give you the abbreviation of the month.  So I made my short date format MMMM dd yyyy and it comes out like this at the cmd prompt.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date%
Dec 07 2011

So if you want the Date spelled out completely you could just do a bunch of IF statements to match the Month and the set a variable to the full month name.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Date by month-day-year in batch file?
« Reply #3 on: December 07, 2011, 08:07:03 PM »
Just remembered I did something similar like this a few weeks ago on another forum.
So again if your date variable outputs like this.
Code: [Select]
C:\Users\Squash\batch\Increment>echo %date%
Wed 12/07/2011
Code: [Select]
@echo off
:: setting v to equal the current numeric month
set v=%date:~4,2%

:: putting all the possible months into a variable which will be parsed in the for loop
set month_text="01 January" "02 February" "03 March" "04 April" "05 May" "06 June" "07 July" "08 August" "09 September" "10 October" "11 November" "12 December"

:: for loop is parsing the month_text variable and using the find command with the numeric month to set the month variable
for %%I in (%month_text%) do echo %%I |find "%v%" &&set month=%%~I
:: Need to remove the Number and space
echo %month:~3% %date:~7,2% %date:~10,4%
It will output this.
Code: [Select]
C:\Users\Squash\batch>getmonth.bat
"12 December"
December 07 2011