5

This does not work:

echo %DATE% %TIME% & timeout 5 & echo %DATE% %TIME%

You'll notice the first and the second values are identical.

2

7 Answers 7

8

Command Line (cmd)

You can wrap the command in a quoted string, enable delayed variable expansion, and use ! so the variable expands at execution time rather than when the line is parsed.

cmd /V:ON /C "echo !DATE! !TIME! & timeout 5 & echo !DATE! !TIME!"

Output

Wed 11/19/2025  0:03:00.45

Waiting for 0 seconds, press a key to continue ...
Wed 11/19/2025  0:03:05.19

Supporting Help
  • cmd /?
/C      Carries out the command specified by string and then terminate

/V:ON   Enable delayed environment variable expansion using ! as the
        delimiter. For example, /V:ON would allow !var! to expand the
        variable var at execution time.  The var syntax expands variables
        at input time, which is quite a different thing when inside of a FOR
        loop.
5
  • 1
    Note that if you're in a cmd.exe started already with cmd /V:ON, this won't work. Commented 2 days ago
  • 1
    Thanks. This is a good solution, if somewhat odd. I can't decide which one is best :( Commented 2 days ago
  • 2
    I focused on a solution for what specifically you asked for if that helps, running right from cmd command line, and per what was in alignment with your example command you provided. @martixy The other answers told you why, explained, said run this way, use a batch, etc. I focused on giving you as close to what you asked for basically, Commented 2 days ago
  • I'm not asking you to defend your answer. Just saying there's more than 1 good answer, so I feel it's not appropriate to select just 1 as "the solution". Commented 17 hours ago
  • @martixy It's all good, it's your choice to choose which to select (or not). It's your question so it's your choice. You can do it later if you wish and need more time or now or never. Thank you for the question regardless! Commented 12 hours ago
4

Use net time instead.

For example, either of the following:

net time \\%userdomain% & timeout 5 & net time \\%userdomain%

or

net time %logonserver% & timeout 5 & net time %logonserver%

produce the following output:

Current time at \\<PCNAME> is 11/18/2025 8:34:30 PM
The command completed successfully.
Waiting for <5... 0> seconds, press a key to continue ...
Current time at \\<PCNAME>is 11/18/2025 8:34:35 PM
The command completed successfully.

You can also use the environment variables \\%USERDOMAIN_ROAMINGPROFILE% and \\%USERDOMAIN%. Note that %logonserver% includes the Windows backslashes of the UNC path.

2
  • For net time \\%userdomain% I get The service has not been started. More help is available by typing NET HELPMSG 2184. What works are net time \\%userDNSdomain% or net time /domain:%userdomain% or simply net time /domain (also net time /rtsdomain, but this takes forever) Commented 2 days ago
  • 1
    Ah, thank you. Simple and can be executed on the terminal, which is what I wanted (and without side effects either). I can't decide which answer to accept... Commented 2 days ago
3

You'll notice the first and the second values are identical.

Of course, as variables in cmd are expanded when the command is parsed. There's EnableDelayedExpansion option that you can play with, but I don't think it's an optimal way of measuring command execution times anyways, too much repetitive typing/copying.

Instead I'd recommend using a prompt modifier like Oh My Posh that can automatically output execution time of last command on the fly with execution time segment.

1
  • 1
    I figured something like that might be going on, but couldn't manage to find a good answer. I'll look into Oh My Posh. Commented 2 days ago
3

As Destroy mentioned, variables like %TIME% are expanded into literal values when the script/command is initially parsed, and unless they are modified in the script, the same literal value will be used both times the variable is evaluated, so what you are really executing is

echo "Tue 11/18/2025" "16:42:51.15" & timeout 5 & echo "Tue 11/18/2025" "16:42:51.15"

if you want the %TIME% to reference the actual current time on the system, you must "delay variable expansion" until the variable is used. This is generally achieved by setting setlocal enabledelayedexpansion prior to evaluating the variable, and "endlocal" when you are done. Also note that for delayed evaluation to work properly you must reference the variable with !varname!instead of %varname% and is only supported in batch scripts.

This works when executed in a batch file:

setlocal enabledelayedexpansion
echo !DATE! !TIME! & timeout 5 & echo !DATE! !TIME!
endlocal

Presumably you are asking how to gather execution time metrics because you are working on an existing CMD batch-based product, but if you have the option, consider another shell to target. Powershell has shipped by default on every (even close to) currently supported version of windows, so there is broad compatibility for powershell scripts across windows devices.

2
  • 2
    Exactly. In Powershell, try (Measure-Command {timeout 5}).TotalSeconds. Or .TotalMilliseconds for better resolution. Commented 2 days ago
  • I bet powershell would be better (tho the comment by Paul doesn't show the output of the command), but I frequently reach for cmd out of habit and wanted something simple that works on the terminal, not in a batch file. (Actually just wanted to time chkdsk, since I noticed it lying to me.) Commented 2 days ago
1

Set your environment as so:
set prompt=$T$H$H$H$H$H$H$S%computername%\%username%$S$M$P$+$G

(remove all $H for hundredths of seconds or use fewer $H to reveal seconds set prompt=$T$H$H$H$S%computername%\%username%$S$M$P$+$G)

precede your command with an echo %time% as in echo %time% & (your command)

You will see who ran the command on which computer, when it started and when it finished.

This answer may require scrolling up through the terminal history to see all the output, or capture/pipe output to a log file if it writes more than the buffer.
If you need duration, subtract the stop and start times

Source: GitHub Gist Prompt.txt

Sample

 9:29 MyPC-11(Admin) C:\Users\me>set prompt=$T$S%computername%\%username%$S$M$P$+$G

 9:30:13.05 MyPC-11\me C:\Users\me>echo %time% & ping -n 12 -w 2500 example.com
 9:31:57.25

Pinging example.com [2600:1408:ec00:36::2738:7d2a] with 32 bytes of data:
Reply from 2600:1408:ec00:36::2738:7d2a: time=46ms
---8<--- cut
Reply from 2600:1408:ec00:36::2738:7d2a: time=66ms

Ping statistics for 2600:1408:ec00:36::2738:7d2a:
    Packets: Sent = 12, Received = 12, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 46ms, Maximum = 71ms, Average = 56ms

 9:32:08.57 MyPC-11\me C:\Users\me>
1

In a command line:

echo %DATE% %TIME% & timeout 1 & call echo ^%DATE^% ^%TIME^%

In a script:

echo %DATE% %TIME% & timeout 1 & call echo %%DATE%% %%TIME%%
0

A solution that calculates how long the command ran, using set /A , substring extraction with ~ and mitigation against leading space in the number of hours.

set "START=!TIME!" & timeout 5 > nul & set "END=!TIME!"
set /A DURATION=(1!END:~0,2!-1!START:~0,2!)*3600 + (!END:~3,2!-!START:~3,2!)*60 + (!END:~6,2!-!START:~6,2!)
echo Command took !DURATION! seconds

Either run inside an interactive cmd /V:ON session or behind setlocal enabledelayedexpansion inside a .bat file.

It assumes the command doesn't run over midnight, if that's a concern add if !DURATION! LSS 0 set /A DURATION_S+=86400.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.