# Solved: Date format in bat file



## bikey33

Hi, 
I need to have a date reformatted so that I can see if a file is in a directory for the current date. 

When I run the following:
@echo off > %temp%\# & setLocal EnableDelayedExpansion
echo %date% 

I get this:
2009-06-20 

I need it to be 2009/06/20

Once i have that I know how to do the rest. I have searched and tried many examples that parse the date out to reformat and nothing seems to work, so I am back to square one.

Your help would be tremendously appreciated!

Thanks


----------



## devil_himself

Code:


@echo off
for /f "tokens=1-3 delims=./-" %%f in ("%date%") do (
    set _today=%%f/%%g/%%h
    echo %_today%
 )


----------



## TheOutcaste

If the delimiter will always be a *-*:


Code:


Set _Today=%date:-=/%
Echo %_Today%

If you want it to be 20090620, use this:


Code:


Set _Today=%date:-=%
Echo %_Today%

Jerry


----------



## bikey33

Hi there, 
That worked great! I just needed to put echo %_today%
outsided of the parenthesis instead of in. Thank you so much. Now I have another problem, as when I tried to implement it on the server (I was using a test one), the current date comes up as:
The current date is: Sun 06/21/2009. 

Is there a way to get rid of the SUN? I can change other variables around (set _today=%%h-%%f-%%g) easily thanks to you , but don't know what to do about that Day variable.

Thanks,
Bikey33


----------



## TheOutcaste

Code:


@Echo Off[COLOR=DarkRed]
If "%Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)[/COLOR]
For /F "Tokens=%_NumTok% Delims=./[COLOR=Red]-[/COLOR] [COLOR=Red]"[/COLOR] %%f In ("%date%") Do Set _Today=%%f/%%g/%%h
)
Echo %_Today%

Added the line in Red and also added a space after Delims.

This will get the date regarless of your Regional settings, and sets the _fdate variable to YYYYMMDD. You can then extract the year, month, and day from _fdate and put it together anyway you want:


Code:


@Echo Off
Set _Date=%date%
If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
Set _Today=%_fdate:~4,2%-%_fdate:~6,2%-%_fdate:~0,4%
Echo %_Today%

HTH

Jerry


----------



## ghostdog74

to use date independent of regional settings,you can use vbscript


Code:


strtoday = Now
yr = Year(strtoday)
mth = Month(strtoday)
dy = Day(strtoday)
If Len(mth) <2 Then 
	mth="0"&mth
End If
If Len(dy) <2 Then 
	dy="0"&dy
End If
thedate = yr &"-"&mth&"-"&dy
WScript.Echo thedate

save the above as myscript.vbs and in your batch


Code:


for /f ..... ( 'cscript /nologo myscript.vbs' ) do (
  .....
)

(Or you can do your task entirely in vbscript)


----------



## bikey33

Hi, Sorry it took so long to reply. Had to focus on other issues and could not test this right away. I used the resolution from The Outcast and it worked great. Thank you so much!


----------



## TheOutcaste

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

