-2

I need to get just HH:MM to the code work but if I do echo %TIME% the variable is HH:MM:SS, how can I break the string or make it show just hours and minutes?

  • TBF, the question seems to be more about variable expansion than how to get the date and time in a specific format, _(although getting a known consistent format should be essential as part of the task)_. I have therefore provided an answer which achieves both the consistent format and the required expansion. – Compo Feb 02 '21 at 15:30

2 Answers2

0
echo %TIME:~0,-3%

Although %time% for me shows 22:23:01.87

so for me,

echo %TIME:~0,-6%

See set /? from the prompt for more info.

Magoo
  • 68,705
  • 7
  • 55
  • 76
0

The biggest issue with your idea is that %TIME% is not output in a consistent format accross locales, PC's, or users. If you want a method which should work regardless of PC or User settings, then the following may help you:

@For /F "Tokens=2-3 Delims=: " %%G In ('""%__AppDir__%Robocopy.exe" \: . /NJH /L | "%__AppDir__%find.exe" " 123""') Do @Echo %%G:%%H

You may be better advised to use the same method to save new variables with the current date and time in a known format first, i.e. yyyy/MM/dd and hh:mm:ss, then use variable expansion on the time one to either the first five characters ~,5, or the everything except for the last three, ~,-3.

@For /F "Tokens=1-2" %%G In (
    '""%__AppDir__%Robocopy.exe" \: . /NJH /L | "%__AppDir__%find.exe" " 123""'
) Do @Set "NowDate=%%G" & Set "NowTime=%%H"
@Set Now & Echo hh:mm is %NowTime:~,5% & Pause

The last line is just included to provide you with example output, you would obviously change that to suit your own purposes.

Compo
  • 30,301
  • 4
  • 20
  • 32