-1

I need the value of the hour and the minute value separately. I need them for some calculations.

I don't need this:

echo %date%

Output: Days/Months/Years;

echo %time%

Output Hours/Minutes/Seconds;

I need:

x

Output: Hours;

y

Output: Minutes;

  • 2
    Does this answer your question? [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) It shows how to parse individual parts of the date into their individual components, so you can modify it to just remove the hours and minutes. – Ken White Mar 25 '21 at 20:01

1 Answers1

0

This is easy enough with the PowerShell that is already on your supported Windows system.

FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command "Get-Date -Format 'HH'"') DO (SET "HOUR=%%~A")
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command "Get-Date -Format 'mm'"') DO (SET "MINI=%%~A")
ECHO HOUR is %HOUR%
ECHO MINI is $MINI

Another way is to use WMIC.exe, but that still requires parsing out the hour and minute using %VAR:x,y^ values. The upside is that X and Y are always the same. Using the TIME variable parsing out the hour and minute can be done. However, the user may have the hour configured to not have a leading zero if the hour is less than 10:00. And there is the problem of AM/PM.

For an extensive list of alternatives, see https://stackoverflow.com/a/19799236/447901

lit
  • 14,456
  • 10
  • 65
  • 119