www.rodneybeede.com "I would love to change the world, but they won't give me the source code" - unknown
 Navigation

Windows Batch Script - Parsing Time Into Components (Hour, Minute, Second) - Last Modified 2008-10-07 21:39 UTC - Created 2008-10-07 21:39 UTC

I found a U.S. locale specific way to parse a time into components inside a Windows batch script (.bat or .cmd).

@REM Valid only for U.S. (English) locale
FOR /F "tokens=1-3 delims=:." %%i IN ('echo %time: =0%') DO SET HOUR=%%i& SET MINUTE=%%j& SET SECOND=%%k

Note that the %time% variable has the leading space (if any, exists when hour < 10) converted to a leading 0.

Again it only works for the U.S. locale (and similiar). If you need something that works regardless of locale there are other examples that do it in a batch script, but you should really consider an external .com or .exe that inserts it into your batch script (Google it) or use a Windows Scripting Host script instead.