# Calculating Time Differences in a Batch Script



## lancastert (Apr 5, 2007)

I am having a problem calculating time differences in batch.

I want my script to work out when a user started a process and when they finished it and calulate how long it took them.

E.g. Time In: 09:02. Time out: 11:52 which works out at 02:50 or 2 Hours 50 Mins.

Does any one know how can I set this up in a Batch script?


----------



## lotuseclat79 (Sep 12, 2003)

You can use the date command (just before the user process is executed) in a script and assign it to a variable, then parse the variable with a nawk script to get the start time. And just after the user process is finished executing in the script, you can repeat the above sequence to get the finish time and then pick off the difference in hours, minutes and seconds by using expr in the bach script.

-- Tom


----------



## lancastert (Apr 5, 2007)

Can you show me an example please?

I was also thinking down this route:

Can I use the SLEEP command to count upto an hour then add 1 to a variable for the hour and so forth until the user presses a certain key which will prompt the script to add a minute value to another variable? Then I can combine the two variables in this format HH:MM


----------



## lotuseclat79 (Sep 12, 2003)

Hi lancastert,

I would not use that sleep command as you described.

To store the date in a the variable named date:
date=`date`

Note, the hh:mm:ss are in the 4th item in the output of the date command, or:
echo $date

To capture capture and print the hh:mm:ss at the start of the script into a variable named stime:
date=`date`
stime=`echo $date | nawk '{print $4}'`
echo $stime

Simlarly, at the end of the user process execution in the script, you can capture the end time with:
date=`date`
etime=`echo $date | nawk '{print $4}'`
echo $etime

To bring it all together since you now know how to capture the start and end time, you need to use nawk with a separator field, ":" in order to separate the hh:mm:ss, and then use the expr facility of the script language (sh, bash) to compute the difference in hours and seconds.

Search for a Shell tutorial and use of expr on the Internet for more information by trying:
"Shell tutorial" +expr

or figure it out by this example if you can (see the man page on expr:

#!/bin/sh
i=1
while [ $i -le 5 ] ; do
echo $i
i=`expr $i +1` # add one to i
done

-- Tom


----------



## devil_himself (Apr 7, 2007)

here a pure ms dos batch script


```
::save as "timediff.bat"
::usage --> timediff hr1 mn1 sc1 hr2 mn2 sc2
::ex --> timediff 12 23 40 20 9 12
@echo off

:: convert input hours to seconds
set /a hh1=60*60*%1
set /a hh2=60*60*%4

:: convert input minutes to seconds
set /a mm1=60*%2
set /a mm2=60*%5

:: input seconds
set /a ss1=%3
set /a ss2=%6

:: add input hours , minutes , seconds 
set /a t1=hh1+mm1+ss1
set /a t2=hh2+mm2+ss2

:: calculate difference in seconds
set /a ds=t2-t1
  
if %ds% LSS 0 set /a ds=%ds%+24*60*60

set /a h=(%ds%/3600)
set /a m=(%ds%/60)-60*%h%
set /a s=%ds%-60*(%ds%/60)

echo hrs=%h% min=%m% sec=%s%
goto :EOF
```


----------

