Link to home
Start Free TrialLog in
Avatar of acdagirl
acdagirl

asked on

how do I calculate time taken between two Datetime's

Given the following, how do I calculate the time difference between timeLineStart and timeLineEnd - or is there a better way than I've tried below?

Dim timeLineStart As DateTime
Dim timeLineEnd As DateTime
Dim timeTaken As DateTime

timeLineStart = System.DateTime.Now

(run a stored procedure....)

timeLineEnd=System.DateTime.Now

timeTaken = ?? (in minutes)

SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Building on the last two comments, you get a TimeSpan instance back from Subtract():

    Dim timeTaken As TimeSpan = DateTime.Now.Subtract(timeLineStart)

Now you can get the individual parts such as Hours, Minutes, Seconds, etc. if you need them:

    Debug.Print(timeTaken.Hours)
    Debug.Print(timeTaken.Minutes)
    Debug.Print(timeTaken.Seconds)

These give you the WHOLE parts whereas the TotalXXX() properties give you fractional values.

For instance, if it took 84 minutes:

  Hours = 1
  Minutes = 24

but:

   TotalHours = 1.4
   TotalMinutes = 84

See the TimeSpan class:
http://msdn2.microsoft.com/en-us/library/system.timespan_members.aspx