PDA

View Full Version : Adding Times in VB.NET


overwing
3rd Sep 2006, 11:54
Hi,
I am trying to add times when I press "Button1" to sum "Textbox1" and "Textbox2" the result to display in "Label1"
e.g. Textbox1 = 12:03 and Textbox2 =02:03 I want Ans. 14:06
Textbox1 = 22:03 and Textbox2 =05:20 I want Ans. 03:23
This is in VB.NET
Can anyone show me the code.
Thanks.
:confused:

BOAC
3rd Sep 2006, 12:25
Cannot help with VB code, but generally it goes:

'If LabelY>X then LabelY=LabelY-X'

In the case of hours, X=24. You'll need to split the minutes out as well (maybe use 'INT' or 'Left/RightString' depending on how VB codes) and then X will be 60. They need adding back in at the end. Might be easier just to work in minutes until the end?

Getting the 'zeroes' in front of the numbers is a whole new ball game....................:)

overwing
3rd Sep 2006, 14:34
Ta, BOAC
That code is ok for things like Excel and VBA. But .NET is different. I'm getting lost somewhere in converting from data type 'string' which is the only thing a textbox will accept in .NET into 'datetime'
I'll keep hunting.

BOAC
3rd Sep 2006, 18:20
OK- my last go since I cannot guess exactly what you are trying to do!

If you just want to add two data strings representing time, why bother with 'datetime'? Perhaps if you outlined the task someone could help?

zoink
3rd Sep 2006, 18:30
overwing

you need to work with a timespan difference.. create a datetime object for each time then get the difference between the 2 then add the difference to the first one


article here in c# on datetime differences but exact principle applies to vb.net

http://articles.techrepublic.com.com/5100-3513_11-5760752.html

overwing
5th Sep 2006, 09:45
It's the Timespan that fixed it.

Textbox1.text with StringToDateConversion function
then .add to (textbox2) with Textbox2 as timespan

Thanks guys,