Need leading zero for batch script using %time% variable
Solution 1
A very simple way is to just replace the leading space with zero:
echo %TIME: =0%
outputs:
09:18:53,45
Solution 2
My Solution was to use the following idea:
SET HOUR=%TIME:~0,2%
IF "%HOUR:~0,1%" == " " SET HOUR=0%HOUR:~1,1%
Solution 3
Similar idea to Dennis' answer. The problem is that the width of %time%
is always the same, so it inserts a space in the beginning instead of returning a shorter string.
You can get rid of that with for
:
for /f "delims= " %x in ("%time%") do set T=0%x
The rest is more or less the same, then.
Solution 4
Using Jesse's Contribution, I just created a variable with the modified output. Then I reference that variable to build the hour portion.
set NantTime=%time: =0%
nant\bin\nant.exe -nologo+ -debug+ -verbose+ -D:project.config=debug /f:build\default.build -l:logs\architect-build-%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%NantTime:~0,2%-%time:~3,2%-%time:~6,2%.log
pause
With the original source:
set hour=%time: =0%
set logfile=C:\Temp\robolog_%date:~-4%%date:~4,2%%date:~7,2%_%hour:~0,2%%time:~3,2%%time:~6,2%.log
Thanks Jesse. I would have voted if I had the reputation points.
Solution 5
The following takes a few more lines but is clear and understandable. It saves stdout and stderr to separate files, each with a timestamp. The timestamp includes year, month, day, hour, minute, and second, in that order. Timestamps should always have the most significant date component first (year) and the least component (seconds) last. That enables files listings to be in time order. Without further ado, here is my solution.
:prepare time stamp
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
set hour=%time:~0,2%
:replace leading space with 0 for hours < 10
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set minute=%time:~3,2%
set second=%time:~6,2%
set timeStamp=%year%.%month%.%day%_%hour%.%minute%.%second%
:run the program
ProgramName.pl 1> RunLogs\out.%timeStamp% ^
2> RunLogs\err.%timeStamp%
Related videos on Youtube
Ira
Updated on September 17, 2022Comments
-
Ira almost 2 years
I came across a bug in my DOS script that uses date and time data for file naming. The problem was I ended up with a gap because the time variable didn't automatically provide leading zero for hour < 10. So running> echo %time% gives back: ' 9:29:17.88'.
Does anyone know of a way to conditionally pad leading zeros to fix this?
More info: My filename set command is:
set logfile=C:\Temp\robolog_%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
which ends up being: C:\Temp\robolog_20100602_ 93208.log (for 9:23 in the morning).
This question is related to this one.
Thanks
-
Ira about 14 yearsIt is possible to get padded hour value... FOR /F "TOKENS=1 DELIMS=:" %%A IN ('TIME/T') DO SET HH=%%A then replace %time:~0,2% with %HH% I was hoping for a more compact solution, but this will work.
-
user1686 about 14 yearsA "more compact" solution would be something in another language (powershell? python? perl? WSH?).
-
Ira about 6 yearsSwitched to mark Jesse's answer as the solution I use. It works with both pre-noon times to pad w/ leading 0 and also post-noon military time.
-
-
Ira about 14 yearsI'm not sure this works. If I run 'set T=0%time%' and it's 9:38AM, echo %T% gives back: '0 9:38:54.21', then taking %T:~1,2% gets the space, no leading 0.
-
Ken over 10 yearsI like it. Simple and intuitive.
-
aerobrain almost 10 yearsit works! this is the simplest solution I think..
-
peterh about 9 yearsAlthough the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long but it is expected.
-
Devolus over 8 yearsThis solution works for > 10 but now creates '010' for 10:...
-
Devolus over 8 yearsCan this also be done with multiple characters? i.E. all ',', ' ' and ':'. Currently I create intermediate variables but of course, this works only because of the few numbers of replacements. For more it would be cumbersome.
-
Dr BDO Adams about 8 yearsWorks on modern windows, but gives syntax error on server 2003!
-
bjoster over 7 yearsWorks flawless on 2008+, most elegant solution. Thank you.
-
Marcin Orlowski over 7 years
but gives syntax error on server 2003
- why you care that in 2016? -
handle over 7 yearsI'm using this in a second "step" to modify the Hours:
set HH=%time:~-11,2% set MM=%time:~-8,2% set SS=%time:~-5,2% set ISOTIME=%HH: =0%-%MM%-%SS% echo %ISOTIME%
Also see ss64.com/nt/syntax-replace.html -
thebunnyrules over 6 yearsThis is a very bad answer, it breaks down as soon as there are two digits in the hour. I can't beleive the OP selected it.
-
B. Shea about 6 years+1 This is shortest/best answer. How I used:
set timestring=%TIME: =0%
early in sub call - then just work withtimestring
instead (or whatever variable you use). -
Mike Upjohn almost 6 yearsPerfect on Windows 10 Command Prompt