Calculate Hours and Minutes

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I would like to calculate the difference between hours and minutes from one textbox and the hours and minutes from textbox two. What would be the best way to do this? I think DateTime would be the object to use, but wondered if anyone has any examples or better ways?

Thanks
 
You can use TimeSpan also.
VB.NET:
Dim time1 = TimeSpan.Parse("10:00")
Dim time2 = TimeSpan.Parse("12:00")
Dim diff = time2 - time1
 
Im using the above code and having some difficulty. An example:

I use the value 2:30 (time1 from the code above) and want to subtract 60 minutes. So i type 0:60 for the second value but i get the error "The TimeSpan could not be parsed because at least one of the numeric components is out of range or contains too many digits.". This makes me believe i have to type 1:00 which works but how could i force this or to limit/stop the user from entering 0:60 or how could i check conditions have been met before calculating? I did try If Time2.Minutes > 59 but this doesnt work as i expected it to.

Thanks
 
TimeSpan.TryParse will let you try to parse the input without it throwing an exception. Return value is Boolean and tells whether conversion was possible, and input parameter is ByRef and returns in that case the converted value.
 
Thanks for your help. Is there anyway to perform this using a Datetime object or to convert from TimeSpan so it returns a DateTime?
 
Yes, you can use Date type (DateTime) instead. Same code, methods and input can be used. When only time is given the date part is set to todays.
 
I didnt grasp the end of your last reply. Ive posted my code as im not sure which part you are suggesting i change?

VB.NET:
Dim CurrentTime As TimeSpan = Nothing
Dim ChangedTime As TimeSpan = Nothing
Dim DifferenceInTime As TimeSpan = Nothing

If TimeSpan.TryParse(CurrentTime, CurrentTime) AndAlso TimeSpan.TryParse(AdjustedTime, AdjustedTime) Then
        DifferenceInTime = CurrentTime - ChangedTime
End If

Return DifferenceInTime
 
Last edited:
Do you know what the two parameters of the TimeSpan.TryParse mean? If not, don't just guess. Look it up. Read the documentation for the TimeSpan.TryParse method so that understand what it does. I'll give you a clue: you're supposed to parsing a String into a TimeSpan. Where have you specified a String in your code?
 
I edited the code above to make it more readable so i made a typo...

In the code

VB.NET:
If TimeSpan.TryParse([B][I][U]CurrentTime[/U][/I][/B], CurrentTime) AndAlso TimeSpan.TryParse([B][I][U]AdjustedTime[/U][/I][/B], AdjustedTime) Then

The bold,italic underlined text should be string, but i notice i passed in the same variable twice in error....
 
I calculate the hours / minutes worked on a project. This is the code I use when the user clicks "Complete Project"

VB.NET:
Dim dt1 As DateTime = Me.lblStartDateTime.Text
Dim dt2 As DateTime = Now
Dim ts As TimeSpan = dt2.Subtract(dt1)

I display the time to the user on a new label (and store ts to my database)
TS will display as a long number, something like 123.10:45:07.1000000

To display this "friendly" you would do something like:
VB.NET:
dim strTime as string
strTime = "Total Time to complete this project: " & " | Days: " + ts.Days.ToString & " | Hours: " + ts.Hours.ToString & " | Minutes: " + ts.Minutes.ToString
me.lblProjectTime.text = strTime
 
Thanks everyone.... Arg81, thanks for your advice but i guess your using a TimeSpan to return a value and then having that value in a string...... I would need the return value to be DateTime, so were "almost there".
 
So you've got 2 TextBoxes with string representations of hours & minutes and you want to find the difference as a DateTime?

I think you need to stop, explain exactly what you're going to be seeing as input values and exactly what you want to get as an output value.
 
So you've got 2 TextBoxes with string representations of hours & minutes and you want to find the difference as a DateTime?

I think you need to stop, explain exactly what you're going to be seeing as input values and exactly what you want to get as an output value.

In basic terms i need to calculate how much time is spent on a particular task. So the end user enter 1 hour and 10 minutes one day and next day 2 hours......everytime a value is entered i need to add the value to total the time up.
 
Back
Top