I think you should use the %TIME%
pseudo-variable.
This is an example:
@echo off
For /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
SET HH24=%%a
SET MI=%%b
SET SS=%%c
SET FF=%%d
)
For /f "tokens=1-2 delims=/,." %%a in ("%SS%") do (
SET JUST_SS=%%a
)
echo HH24=%HH24%
echo MI=%MI%
echo SS=%SS%
echo FF=%FF%
echo JUST_SS=%JUST_SS%
echo mytime=%HH24%-%MI%-%JUST_SS%
Well, I realized HH24 has an empty space if the hour is less than 10, so here you have another solution, using %time
:
SET HH=%time:~0,2%
if "%HH:~0,1%" == " " SET HH=0%HH:~1,1%
echo HH=%HH%
SET MI=%time:~3,2%
if "%MI:~0,1%" == " " SET MI=0%MI:~1,1%
echo MI=%MI%
SET SS=%time:~6,2%
if "%SS:~0,1%" == " " SET SS=0%SS:~1,1%
echo SS=%SS%
Following the advice pointed out by @Stephan (TIME
variable is dependent on region settings), we can use this approach:
@echo off
for /f "delims=" %%a in ('wmic OS get localdatetime ^| find "."') do set datetime=%%a
set "YYYY=%datetime:~0,4%"
set "MM=%datetime:~4,2%"
set "DD=%datetime:~6,2%"
set "HH=%datetime:~8,2%"
set "MI=%datetime:~10,2%"
set "SS=%datetime:~12,2%"
set fullstamp=%YYYY%-%MM%-%DD%-%HH%-%MI%-%SS%
echo fullstamp=%fullstamp%
date
andtime
.