Adding time to time

grags

Active member
Joined
Mar 26, 2005
Messages
26
Programming Experience
Beginner
Ok this is probably one of the most noobiest questions ever. But i've been at it for hours and not been able to figure it out?

How do you add time to time?

Example: 0:54 + 0:06 = 1:00
Example2: Time = cdate(string(1)) + cdate(string(2))

I know the answer is probably the easiest ever, but hey they're only easy if you know the answers right?

Im using VB.Net 2003


Thanks
Grags
Graham Maddocks
 
If you have an instance of the DateTime type then you can use all the methods in there to add minutes, hours, days etc

VB.NET:
Dim Dt As New DateTime(Arguments.....)
 
Dt.Addminutes...
 
Dt.AddSeconds....
 
Thanks for your reply but it just aint working

here's my code.

Dim lviY As Integer
Dim lvdT As New DateTime("hh:mm:ss") 'I think this is wrong
For lviY = 1 To 20
lvdT.AddMinutes(CDate(lblTimes(1, lviY).Text)) 'Expects somthing else here
lvdT.AddSeconds(CDate(lblTimes(1, lviY).Text)) 'Expects somthing else here
Next

MsgBox(CStr(lvdT))
 
Last edited:
I think you should be using TimeSpan objects instead of DateTime objects:
VB.NET:
Dim time1 As New TimeSpan(0, 0, 54)
Dim time2 As New TimeSpan(0, 0, 6)
Dim time3 As TimeSpan = time1 + time2

MessageBox.Show(time3.TotalSeconds.ToString)
If you're only interested in the time portion of a Date object then you can use its TimeOfDay property, which returns a TimeSpan object that represents only the time portion of the DateTime object.
 
Thanks for you reply I appreciate the fact that your trying to help me. But I Tried your code and it gives this error

Operator + is not defined for types 'system.timespan' and 'system.timespan'
 
The "+" operator IS defined for TimeSpans in .NET 2.0, which your profile says that you're using. I saw that and didn't notice that you've mentioned that you're using .NET 1.1 for this. To add two TimeSpan objects you use the TimeSpan.Add method:
VB.NET:
Dim time3 As TimeSpan = time1.Add(time2)
 
Back
Top