Windows – Access to the current time in windows batch script

scriptingtimewindows

I need access to the current time from within a batch script. When typing time in the cmd, I get something like this:

The current time is: 16:58:03.98
Enter the new time:

What I need is not that but the hour, the minutes and the seconds in seperate buckets that I can then process in any arbitrary way. In other words I am looking for a function that returns the value for the hour so that I can assign it to a variable, like so:

var hour = GetCurrentHour()

Thanks!

Best Answer

FOR /F "TOKENS=1 DELIMS=:" %%A IN ('TIME/T') DO SET HH=%%A
FOR /F "TOKENS=2 DELIMS=:" %%A IN ('TIME/T') DO SET MM=%%A

now you have two variables %HH% for hours and %MM% for minutes. Hope this helped.