How-to: Return the current Time - GetTime.cmd

Returns the current time into the variable %_time%

  @echo off
  For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do set "delims=%%a%%b%%c"
  For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
    Set _hh=%%G
    Set _min=%%H
    Set _ss=%%I
    Set _ms=%%J
  :: Strip any leading spaces
  Set _hh=%_hh: =%

  :: Ensure the hours have a leading zero
  if 1%_hh% LSS 20 Set _hh=0%_hh%

  Echo The time is:   %_hh%:%_min%:%_ss%
  ENDLOCAL&Set _time=%_hh%:%_min%

Although the default TIME delimiter is either . or : users can change the delimiter to just about any character they like. It is rare, but some people will change it.

Based on the discussion thread here, this script always returns HH:MM no matter which time delimiter has been set in the control panel. Thanks to avery_larry and bluesxman for their improvements. To include Milliseconds just tack on the %_ms% variable.

“What may be done at any time will be done at no time” ~ Scottish Proverb

Related commands

How-to: DateTime - Get Date, Time and daylight savings (VB Script).
%TIME% - variable containing current time.
TIME - Set the time (list of default time formats for different countries).
GetDate.cmd - Get todays date (any region, any OS).
tdiff.cmd - Time difference in Hours/Minutes. - date/time scripts.
Rob Vanderwoude - date/time scripts.

Copyright © 1999-2023
Some rights reserved