Skip to content

Instantly share code, notes, and snippets.

@topas
Created April 4, 2012 08:00
Show Gist options
  • Save topas/2299597 to your computer and use it in GitHub Desktop.
Save topas/2299597 to your computer and use it in GitHub Desktop.
Batch: Date and time in custom format
:: http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us
@ECHO off
set v_day=
set v_month=
set v_year=
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
::DEBUG echo toks=%toks%
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
::DEBUG echo first token=%%a
if "%%a" GEQ "A" (
for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
set '%%a'=%%i
set '%%b'=%%j
set 'yy'=%%k
)
)
)
if %'yy'% LSS 100 set 'yy'=20%'yy'%
set Today=%'yy'%-%'mm'%-%'dd'%
ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%
::DEBUG ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
set datestring=%V_Year%%V_Month%%V_Day%
SETLOCAL ENABLEEXTENSIONS
for /f "tokens=1-4 delims=:,.-/ " %%i in ('echo %time%') do (
set 'hh'=%%i
set 'mm'=%%j
set 'ss'=%%k
set 'ff'=%%l)
ENDLOCAL & SET v_Hour=%'hh'%& SET v_Minute=%'mm'%& SET v_Second=%'ss'%& SET v_Fraction=%'ff'%
IF 1%V_Hour% LSS 100 SET V_Hour=0%V_Hour%
IF 1%v_Minute% LSS 100 SET V_Hour=0%v_Minute%
IF 1%v_Second% LSS 100 SET V_Hour=0%v_Second%
::DEBUG ECHO Now is Hour: [%V_Hour%] Minute: [%V_Minute%] Second: [%v_Second%] Fraction: [%v_Fraction%]
set timestring=%V_Hour%%V_Minute%%v_Second%
echo %datestring%%timestring%
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment