Skip to content

Instantly share code, notes, and snippets.

@maciakl
Created February 25, 2013 21:44
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maciakl/5033614 to your computer and use it in GitHub Desktop.
Save maciakl/5033614 to your computer and use it in GitHub Desktop.
Windows batch files - date and time formatting:
@echo off
rem System provided date and time
echo %DATE%
echo %TIME%
rem Capture Hour
set hour=%time:~0,2%
rem Remove leading space if single digit
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
rem Minutes
set min=%time:~3,2%
rem Remove leading space
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
rem Seconds
set secs=%time:~6,2%
rem Remove leading space
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%
rem Year
set year=%date:~-4%
echo year=%year%
rem Month
set month=%date:~3,2%
rem Remove leading space if single digit
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
rem Day
set day=%date:~0,2%
rem Remove leading space
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%
rem Reformatted date
set datetimef=%year%-%month%-%day% %hour%-%min%-%secs%
echo datetimef=%datetimef%
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment