Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time manipulations

Status
Not open for further replies.

russgreen

Programmer
Dec 7, 2002
86
GB
Hi,

I'm trying to perform several manipulations to time values (all will be stored in a access database). I need to find the elapsed time between a start time and a finish time and I also need to be able to find the SUM of multiple time values.

I know how to do this first part of the using the following code but how do I add multiple time values together when they are in the "15:53:12" format?

TIA

Russ

Dim TimeStart, TimeStop As Date
Dim TimeDiff As TimeSpan

TimeStart = CDate(Me.txtTimeStart.Text)
TimeStop = CDate(Me.txtTimeStop.Text)

TimeDiff = TimeStop.Subtract(TimeStart)

Regards,
Russ

 

You can add multiple times this way:


Dim time1 As DateTime
Dim time2 As DateTime
Dim time3 As DateTime

time1 = "12/30/03 15:53:12"
time2 = "12/30/03 17:53:12"

time3 = "12/30/03 1:00:00"

time3 = time3.AddHours(CDbl(time1.Hour) + CDbl(time2.Hour))

time3 = time3.AddMinutes(CDbl(time1.Minute) + CDbl(time2.Minute))

time3 = time3.AddSeconds(CDbl(time1.Second) + CDbl(time2.Second))



-Garth
 
OK. That kind of does what I want but I'm after something subtly different.

Basically, I'm writing a small db app (using an access 2k backend) that will log time spent on an activity (a log book). Each record will have a Start Time and a Finish Time and an Elapsed Time (using the method in my first post. What I'm trying to get to is the total time spent across all entries. There may only be 20 entries in the database but then there may be 100's. There must be a quicker way to get this value.

Russ

Regards,
Russ

 
Have you tried subtracting the two DateTime variables? One of the the subtraction operator overloads returns a TimeSpan object:
Code:
Dim time1, time2 As DateTime
Dim diffTime As TimeSpan

time1 = New DateTime(2003, 12, 30, 15, 53, 12)
time2 = New DateTime(2003, 12, 30, 17, 53, 12)

diffTime = time2 - time1

The addition operator is also defined:
Code:
Dim time1, time2 As DateTime
Dim incTime As TimeSpan

time1 = New DateTime(2003, 12, 30, 15, 53, 12)
incTime = New TimeSpan(2, 0, 0)

time2 = time1 + incTime
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
There is the structure system.timespan made just for elapsed time like you need. I include a sample code that may suggest how it can be used.
Hope this helps.


Dim timeSpan1 As TimeSpan
Dim dt1 As DateTime = DateTime.Now
System.Threading.Thread.Sleep(1500)
timeSpan1 = DateTime.Now.Subtract(dt1)
ListBox1.Items.Add("timespan=" & timeSpan1.ToString)
Dim timeSpan2 As TimeSpan
Dim dt2 As DateTime = DateTime.Now
System.Threading.Thread.Sleep(1500)
timeSpan2 = DateTime.Now.Subtract(dt2)
ListBox1.Items.Add("timespan=" & timeSpan2.ToString)
ListBox1.Items.Add("TOTAL=" & (timeSpan1.Add(timeSpan2)).ToString)
' the string representation of timespan is
' [-]d:hh:mm:ss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top