2

I am aware that one can print batch time up to the centisecond with @echo %time%. Is there a similar command to get milliseconds as well?

John Roberts
  • 5,885
  • 21
  • 70
  • 124
  • 1
    Is your batch file so fast that 10 milliseconds (5, on average) will make a difference ? – ixe013 Oct 24 '12 at 16:50
  • 1
    There's nothing built-in to the command shell to get the time to the millisecond, but powershell has the Get-Date command which returns an object that contains milliseconds – Anya Shenanigans Oct 24 '12 at 17:09

5 Answers5

1

There is a resource kit utility named Timethis that provides up to the millisecond time measurements :

TimeThis :  Command Line :  dir
TimeThis :    Start Time :  Wed Oct 24 12:49:56 2012
TimeThis :      End Time :  Wed Oct 24 12:49:56 2012
TimeThis :  Elapsed Time :  00:00:00.093
ixe013
  • 9,559
  • 3
  • 46
  • 77
1

Use wmic os get LocalDateTime. That's also the reliable way to get locale-independent date and time

for /F "usebackq tokens=2 delims==" %%i in (
    `wmic os get LocalDateTime /VALUE`
) do @set ctime=%%i
echo milliseconds: %ctime:~15,3%
phuclv
  • 37,963
  • 15
  • 156
  • 475
0

(Edit: doh, didn't notice that this has the windows tag. Leaving the answer here in case anyone else clicked on this for Linux)

If you are asking to time the execution length of a program or script, precede the command with time, for example:

time ls -R

and this will give you execution time in milliseconds:

real    0m0.667s
user    0m0.000s
sys     0m0.144s

If you are asking for the current date and time:

Take a look at:

man date

For example,

date +"%X %N"

This will give you the current time in Hour:Minutes:Seconds and then the Nanoseconds

Hope this answers your question

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0

Gnu gdate can make this very simple; try the code below in a batch file, for instance. I use it to create unique filenames for batch processes invoked in command windows, and on my system under Windows 7 it's evident that, at least in a command window, two immediately successive uses of gdate never give identical results.

gdate +"%%Y%%m%%d%%H%%M%%S%%N"
gdate +"%%Y%%m%%d%%H%%M%%S%%N"

set i=
set T=
for /f %%i in ('gdate +%%Y%%m%%d%%H%%M%%S%%N') do set UniqueTime=%%i

echo %UniqueTime%

But I actually use something more like the line below, discarding the last five digits because for my purposes they never seem to matter: the preceding part is always unique. This lets the unique file names be shorter.

gdate +"%%Y%%m%%d%%H%%M%%S%%N" | sed "s/.....$//"

Pretty obviously, the same expression works under Linux, so one can also use it there.

djc
  • 21
  • 1
  • 3
0

ptime utility is "5ms or better" accurate

ptime echo hallo

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

===  echo hallo ===
hallo

Execution time: 0.022 s
user2956477
  • 1,208
  • 9
  • 17