format date

  • Thread starter Thread starter Vladimir
  • Start date Start date
V

Vladimir

Hi, how can I do to specify the format date mmddyyyy in a bat file because I need to create a file with this variable?

thanks
 
I found this "magic" a while back:

@echo off
@rem NEWSGROUP: microsoft.public.win2000.cmdprompt.admin
@rem SUBJECT : How can i to get the current month in commandline?
@rem WHEN/WHO : Mar 14 2001, 9:05 pm post by Michael (maj0)

:: Tokenise date into dd mm and yy independent of locale
for /f "tokens=2-4 delims=.:/-, " %%i in ("%date%") do (
for /f "tokens=2-4 delims=/-,() skip=1" %%l in ('echo.^|date') do (
set %%l=%%i
set %%m=%%j
set %%n=%%k))


:: Lets see what we got!
for %%i in (dd mm yy) do set %%i

Bye,
Dennis Bareis
 
Here is how I get this done:

FOR /F "tokens=1-4 delims=/ " %%a IN ('date /t') DO (
set DayOfWeek=%%a
set mm=%%b
set dd=%%c
set yyyy=%%d
set newDate=%%a %%b/%%c/%%d )

FOR /F "tokens=1-3 delims=: " %%n IN ('time /t') DO (
set hour=%%n
set mins=%%o
set ampm=%%p )

I hope this helps.

Dale

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hi, how can I do to specify the format date mmddyyyy in a bat file because I
need to create a file with this variable?

thanks
 
You can also simply

set MMDDYYYY=%date:~4,2%%date:~7,2%%date:~10,4%
echo.%MMDDYYYY%

Hi, how can I do to specify the format date mmddyyyy in a bat file because I need to create a file with this variable?

thanks
 
dbareis said:
I found this "magic" a while back:

@echo off
@rem NEWSGROUP: microsoft.public.win2000.cmdprompt.admin
@rem SUBJECT : How can i to get the current month in commandline?
@rem WHEN/WHO : Mar 14 2001, 9:05 pm post by Michael (maj0)

:: Tokenise date into dd mm and yy independent of locale
for /f "tokens=2-4 delims=.:/-, " %%i in ("%date%") do (
for /f "tokens=2-4 delims=/-,() skip=1" %%l in ('echo.^|date') do (
set %%l=%%i
set %%m=%%j
set %%n=%%k))


:: Lets see what we got!
for %%i in (dd mm yy) do set %%i

Recently i've come somewhat interested in this topic, so i've
found this group and this discussion as well as the cmd-script
(a degree better then the one above) in the Microsoft FAQ:
http://www.microsoft.com/technet/prodtechnol/Windows2000serv/support/FAQW2KCP.mspx
However, it does not provide true locale independent solution too.
So i've spent last evening to create a more universal design.
This is primarily destined for non-English users. If someone is
interested in that, please test it.

The idea is based on the fact that "chcp 437" switches output
of the "date" command into English. That's true for Windows XP
with MUI pack. However, it appears to be not true for a fully
localized Windows 2000. For the latter case the script needs
some easy modification - see code below (actually, i did not
test it under other Windows versions).

--
@echo off

rem The commandline script creates locale independent
rem environment variables for current date and time.
rem This works under Windows XP with MUI pack al least.
rem
rem USAGE: this.cmd <Prefix>
rem
rem On return the following environment variables set:
rem <Prefix>.Y year number (00-99)
rem <Prefix>.M month number
rem <Prefix>.D day number
rem <Prefix>.H hour (00-23)
rem <Prefix>.N minute
rem <Prefix>.S second
rem <Prefix>.U hundredths of second
rem The values are always in the format of two digits.



:codepage_change
for /f "tokens=2 delims=:" %%a in ('chcp') do set z$zP=%%a
set z$zP=%z$zP: =%
if "%z$zP%"=="437" goto codepage_enu
if "%z$zP%"=="850" goto codepage_enu
chcp 437 >nul
goto time_boogie
:codepage_enu
set z$zP=


:time_boogie
for /f "tokens=2-5 delims=:/-,()." %%a in ('echo.^|time') do (
set z$zH=%%a
set z$zN=%%b
set z$zS=%%c
set z$zU=%%d
goto time_proof )
:time_proof
set z$zH=%z$zH: =%
if "%z$zH:~1%"=="" (
set %1.H=0%z$zH%
) else (
set %1.H=%z$zH%
)
rem set z$zN=%z$zN: =%
if "%z$zN:~1%"=="" (
set %1.N=0%z$zN%
) else (
set %1.N=%z$zN%
)
rem set z$zS=%z$zS: =%
if "%z$zS:~1%"=="" (
set %1.S=0%z$zS%
) else (
set %1.S=%z$zS%
)
set z$zU=%z$zU: =%
if "%z$zU:~1%"=="" (
set %1.U=0%z$zU%
) else (
set %1.U=%z$zU%
)
:time_cleanup
set z$zH=
set z$zN=
set z$zS=
set z$zU=
:time_finish


:date_boogie
for /f "tokens=1 delims=/.,\:-) " %%u in ('date /t') do set z$zD=%%u
if "%z$zD:~0,1%" GTR "9" ( set z$zT=2-4 ) else ( set z$zT=1-3 )
for /f "tokens=%z$zT% delims=/.,\:-) " %%u in ('date /t') do (
for /f "skip=1 tokens=2 delims=(" %%g in ('echo.^|date') do (
for /f "tokens=1-3 delims=/.,\:-) " %%x in ("%%g") do (
if "%%x"=="??" goto date_hard
set z$z%%x=%%u
set z$z%%y=%%v
set z$z%%z=%%w
)
)
)
:date_proof
if "%z$zyy:~3%"=="" (
if "%z$zyy:~1%"=="" (
set %1.Y=0%z$zyy%
) else (
set %1.Y=%z$zyy%
)
) else (
set %1.Y=%z$zyy:~2%
)
if "%z$zmm:~1%"=="" (
set %1.M=0%z$zmm%
) else (
set %1.M=%z$zmm%
)
if "%z$zdd:~1%"=="" (
set %1.D=0%z$zdd%
) else (
set %1.D=%z$zdd%
)
:date_cleanup
set z$zyy=
set z$zmm=
set z$zdd=
set z$zD=
set z$zT=
:date_finish


:codepage_restore
if not "%z$zP%"=="" (
chcp %z$zP% >nul
set z$zP=
)
goto end_boogie




rem The system appears to be unable to set code page 437
:date_hard
set z$z??=
:date_hard_codepage_restore
chcp %z$zP% >nul
:date_hard_boogie
for /f "tokens=1 delims=/.,\:-) " %%u in ('date /t') do set z$zD=%%u
if "%z$zD:~0,1%" GTR "9" ( set z$zT=2-4 ) else ( set z$zT=1-3 )
for /f "tokens=%z$zT% delims=/.,\:-) " %%u in ('date /t') do (
for /f "skip=1 tokens=2 delims=(" %%g in ('echo.^|date') do (
for /f "tokens=1-3 delims=/.,\:-) " %%x in ("%%g") do (
set z$z%%x=%%u
set z$z%%y=%%v
set z$z%%z=%%w
)
)
)
:date_hard_codepages_by_hand
if "%z$zP%"=="866" (
set z$zyy=%z$zгг%
set z$zmm=%z$zмм%
set z$zdd=%z$zдд%
set z$zгг=
set z$zмм=
set z$zдд=
goto date_proof
)
rem JUST ADD YOUR CODEPAGE HERE (CP_OEM)
if "%z$zP%"=="???" (
set z$zyy=%z$z??%
set z$zmm=%z$z??%
set z$zdd=%z$z??%
set z$z??=
set z$z??=
set z$z??=
goto date_proof
)
:date_hard_cleanup
set z$zyy=
set z$zmm=
set z$zdd=
set z$zD=
set z$zT=
echo BUMM !!! ADD YOUR CODEPAGE (%z$zP%), PLEASE
set z$zP=
:date_hard_finish


:end_boogie
rem set > set.txt
--
 
Eman said:
The idea is based on the fact that "chcp 437" switches output
of the "date" command into English.

I've found it does not always work under "nt authority\system" user
profile (the first line of "cmd /c date" output does not correspond
to the "mm-dd-yy" in the second one, even for en-native code pages).
One evening has had not appear to be enough..

Here it goes. This one works just fine :)

--
@echo off


rem The commandline script creates locale independent
rem environment variables for current date and time.
rem
rem USAGE: this.cmd <Prefix>
rem
rem On return the following environment variables set:
rem <Prefix>.Y year number (00-99)
rem <Prefix>.M month number
rem <Prefix>.D day number
rem <Prefix>.H hour (00-23)
rem <Prefix>.N minute
rem <Prefix>.S second
rem <Prefix>.U hundredths of second
rem The values are always in the format of two digits.
rem
rem Env. variable names temporary used:
rem z$zH, z$zN, z$zS, z$zU, z$zY, z$zM, z$zD


:time_boogie
set z$zH=00& set z$zN=00& set z$zS=00& set z$zU=00
for /f "tokens=1* delims= " %%1 in ('echo.^|time') do (
call :time_search %%2 & goto time_proof
)
echo.ERROR: CMD /C TIME error.& goto time_proof
:time_search
for /f "tokens=1-4 delims=:-.," %%1 in ("%1") do (
if "%%3"=="" shift & goto time_search
set z$zH=%%1& set z$zN=%%2& set z$zS=%%3
if "%%4"=="" (set z$zU=%2) else (set z$zU=%%4)
goto :eof
)
echo.ERROR: CMD /C TIME invalid output.& goto :eof
:time_proof
if "%z$zH:~1%"=="" (set %1.H=0%z$zH%) else (set %1.H=%z$zH%)
if "%z$zN:~1%"=="" (set %1.N=0%z$zN%) else (set %1.N=%z$zN%)
if "%z$zS:~1%"=="" (set %1.S=0%z$zS%) else (set %1.S=%z$zS%)
if "%z$zU:~1%"=="" (set %1.U=0%z$zU%) else (set %1.U=%z$zU%)
:time_cleanup
set z$zH=& set z$zN=& set z$zS=& set z$zU=
:time_finish


:date_boogie
set z$zM=00& set z$zD=00& set z$zY=00
for /f "tokens=2*" %%1 in ('net time \\%computername%') do (
call :date_search %%2 & goto date_proof
)
echo.ERROR: NET TIME error.& goto date_proof
:date_search
for /f "tokens=1-3 delims=\/" %%1 in ("%1") do (
if "%%3"=="" shift & goto date_search
set z$zM=%%1& set z$zD=%%2& set z$zY=%%3& goto :eof
)
echo.ERROR: NET TIME output.& goto :eof
:date_proof
set z$zY=%z$zY:~-2%
if "%z$zY:~1%"=="" (set %1.Y=0%z$zY%) else (set %1.Y=%z$zY%)
if "%z$zM:~1%"=="" (set %1.M=0%z$zM%) else (set %1.M=%z$zM%)
if "%z$zD:~1%"=="" (set %1.D=0%z$zD%) else (set %1.D=%z$zD%)
:date_cleanup
set z$zY=& set z$zM=& set z$zD=
:date_finish


:end_boogie
rem set > set.txt
--
 
Back
Top