1

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!

raoulsson
  • 4,633
  • 10
  • 32
  • 29

6 Answers6

6

Time and date are defined in two system variables: %time% & %date%

(Beware, the format depends of the regionnal & language settings)

C:>echo %time%

17:16:13,39

C:>echo %date%

28/08/2009

C:>

Benoit
  • 3,499
  • 1
  • 18
  • 17
  • 1
    The problem I get with this approach is those values never change - for instance if logging the start and end of a long running action, you'll get the same time reported at both points. – asc99c Oct 03 '18 at 13:10
  • @asc99c To get the actual values in a batch loop, use `setlocal enabledelayedexpansion` and `!time!` (note: ! instead of %) – lmagyar Jan 16 '22 at 16:46
4
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.

Merstzik
  • 66
  • 3
2

Combining all of those posts yields an answer to the original question:

for /f "tokens=1-3 delims=:." %%A in ("%time%") do (

set HH=%%A

set MM=%%B

set SS=%%C)

Will load your HH MM and SS buckets with the respective values. You can, of course, type whatever variable names you want in place of those. (Barring reserved names.) You can also leave out the "." in the delims section, if you want fractional seconds to be stored in the last value. (%time% contains HH:MM:SS.ss)

Jerry
  • 31
  • 1
1

It's dangerous to try to do this in CMD, because the result of TIME/T or %TIME% is locale dependent. If you are set on a single locale, you can do this:

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

However, you'd be much better off using PowerShell. CMD scripts are hard to write robustly. There's a lot to learn about writing good CMD scripts, but you'd be better off applying those brain cells to PowerShell.

$hour = (Get-Date).Hour
Jay Bazuzi
  • 663
  • 5
  • 14
  • To offer my CMD creds: I once wrote a multi-threaded, self-updating CMD script that would elevate itself to Adminstrator. I was the only person that could maintain it, which is a bad thing. – Jay Bazuzi Dec 24 '11 at 03:36
1

What you have to do is basically use the %time% variable and specify to count a certain number of characters into the string to extract the part you want. Typing "echo %time%" in a cmd prompt returns something like this:

11:11:38.36

To get just the hour out of this string, type "echo %time:~-11,2%" to display just the first 2 characters. You are basically telling it to display the %time% variable - "echo %time...", count 11 characters backwards from the end of the string - "...:~11..." and grab 2 characters from that point - "...,2%"

To grab the minutes the command would be "echo %time:~8,2%"

To set the variable "hour" to the current hour you would type this:

set hour=%time:~-11,2%

You can also string parts together to create a custom format in whatever combo you need also. For example, to add dashes instead of a colon between the HH:MM:SS you would type this:

"echo %time:~-11,2%-%time:~-8,2%-%time:~-5,2%"

and get: HH-MM-SS

Hope this helps. We use the %date% variable and parts of it all the time to automatically create files in whatever string combo we need the filename to be in.

August
  • 3,114
  • 15
  • 17
  • 1
    Why do you count from the end? Why not do "echo %time:~0,2%" for the hour and "echo %time:~3,2%" for the minutes? – Dennis Williamson Aug 28 '09 at 15:53
  • good point - i don't know, that is the way we have been doing it in our batches forever and I guess it never occurred to me to start from the beginning. I thought you had to count from the end, but this makes it a bit easier - thanks! – August Aug 28 '09 at 16:34
1

Combining Merstzik's and Benoit's answer and saving a call to an external program, you can do this:

FOR /F "TOKENS=1 DELIMS=:" %%A IN ("%TIME%") DO SET HH=%%A
FOR /F "TOKENS=2 DELIMS=:" %%A IN ("%TIME%") DO SET MM=%%A
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148