# Solved: Extracting the Month and Day with batch...



## Codiah (Sep 27, 2008)

Hello again everyone, this is the last piece of my puzzle to my current script i'm working on and i'm having a little trouble with it as well..

what i want to do is extract the month and day from the %DATE% variable and store it in another variable...

when you ECHO %DATE% it outputs the date in this format..

Sun 10/19/2008

What i want to do is extract the month and date, 10/19

and store it in a new variable, %MDVAR%

is it possible using batch scripting?


----------



## Codiah (Sep 27, 2008)

I figured it out, so you can ignore this subject now...

my solution was this..


```
@ECHO off
set _month_day=%DATE:~4,5%
ECHO %_month_day%
```
I i understand this doesn't work the same on Win XP home as it does not have the same standard date format...


----------



## TheOutcaste (Aug 8, 2007)

This handy little routine will get the date from the system regardless of your settings (MM/DD vs DD/MM) or delimiter (/ or - or whatever)
Note that you will have to manually edit the parts in red after pasting because pasting will convert the tab to spaces

```
:GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
For /F "TOKENS=1* DELIMS= " %%A In ("%date%") Do Set _DATE=%%B
[B][COLOR=Blue]:: Delims is a TAB followed by a space[/COLOR][/B].
[B]For /F "TOKENS=2* [COLOR=Red]DELIMS=     "[/COLOR] %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2* [COLOR=Red]DELIMS=     "[/COLOR] %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B[/B]
IF %_iDate%==0 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%B%%C%%D
Echo It's %DATE% today
Echo For sorting purposes: %_fdate%
```
Edit each Set _fdate line to give just the info you want, or parse the result to extract what you want.
_sDate will be the separator character, so you could use
Set _month_day=%_fdate:~4,2%%_sDate%%_fdate:~:6,2%
And it should give the same result on any system, as long as %date% includes the day of the week.
Going to have to add a check for that so it will be truly global

HTH

Jerry


----------



## TheOutcaste (Aug 8, 2007)

Here's a version that should work on any system.I had that first For statement to capture the day of the week so I could check for it, but never added the check

```
:GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified by TheOutcaste from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
@Echo Off
For /F "TOKENS=1* DELIMS= " %%A In ("%date%") Do Set _DATE=%%B
[B][COLOR=Blue]:: Delims is a TAB followed by a space[/COLOR][/B].
For /F "TOKENS=2* [COLOR=Red][B]DELIMS=     "[/B][/COLOR] %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2* [COLOR=Red][B]DELIMS=     "[/B][/COLOR] %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF [%_DATE%]==[] (
    IF %_iDate%==0 For /F "TOKENS=1-3* DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=1-3* DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=1-3* DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
  ) ELSE (
    IF %_iDate%==0 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=1-4* DELIMS=%_sDate% " %%A In ("%date%") Do Set _fdate=%%B%%C%%D
  )
Echo It's %DATE% today
Echo For sorting purposes: %_fdate%
```
Jerry


----------

