eldoktor007

HI my friend ...

I want to know how to sum time like

Case 1

======

How To Do ..

if i have MaskedTextBox included 11:00 Am

and i have onther MaskedTextBox included 1:30 Pm

How i get result be 2:30 hours

Case 2

======

How To Do ..

if i have ListBox Included 5:01 , 4::30 , 6:10 , 3:20

5:01

4:30

6:10

3:20

====== +

Sum Reslt

H 18: M 61

Convert To Hours

H 19 : M 01

How to Do this Operation with Code VBnet 2005

ANSWER TO ME FAST PLEASE

THANX TO TRY HELP ME

AND THANX FOR READ




Re: Visual Basic Language How To Sum Time...?

Brendan Grant

We can use CDate() to convert the Text of your two TextBoxs to DateTime instances and then use the Subtract() method on one to subtract the other from it creating a TimeSpan instance which we can ask for the total hours:

Code Snippet

Dim diff As TimeSpan = CDate(TextBox2.Text).Subtract(CDate(TextBox1.Text))
MsgBox(diff.TotalHours)

You can use a similar method (which I'll leave for you to do) to go through each item in your ListBox, create a DateTime instance and use that to add to an existing counter.

The nifty thing about TimeSpan is that it does the work internally of keeping the format right so that 61 minutes = 1 hour and 1 minute.






Re: Visual Basic Language How To Sum Time...?

TaDa

This should get you rolling...

Code Snippet

Dim D1 As DateTime = #9/6/2007 1:00:00 PM#

Dim D2 As DateTime = #9/6/2007 4:00:00 PM#

Dim Hours1 As String = "5:10"

Dim Hours2 As String = "2:40"

Dim TS1 As New TimeSpan(Integer.Parse(Hours1.Split(":"c)(0)), Integer.Parse(Hours1.Split(":"c)(1)), 0)

Dim TS2 As New TimeSpan(Integer.Parse(Hours2.Split(":"c)(0)), Integer.Parse(Hours2.Split(":"c)(1)), 0)

Dim TS3 As TimeSpan = TS1.Add(TS2)

MsgBox(D2.Subtract(D1).TotalMinutes.ToString)

MsgBox(TS3.Hours.ToString & " " & TS3.Minutes.ToString & " " & TS3.TotalHours.ToString & " " & TS3.TotalMinutes.ToString)





Re: Visual Basic Language How To Sum Time...?

kleinma

There may be more than one way to accomplish either tasks.


Here is one example for each:

Code Snippet

'GET DIFFERENCE BETWEEN 2 TEXTBOX TIMES

Dim StartTime As DateTime = DateTime.Parse(TextBox1.Text) '"11:00 AM"

Dim EndTime As DateTime = DateTime.Parse(TextBox2.Text) '"1:30 PM"

Dim TimeDifferent As TimeSpan = EndTime.Subtract(StartTime)

MessageBox.Show(TimeDifferent.ToString())

Code Snippet

'ADD UP TIMES IN LISTBOX

Dim MyDateTime As DateTime = Nothing

Dim MyTimeSpan As New TimeSpan

For Each S As String In ListBox1.Items

MyDateTime = DateTime.Parse(S)

MyTimeSpan = MyTimeSpan.Add(New TimeSpan(MyDateTime.Hour, MyDateTime.Minute, 0))

Next

MessageBox.Show("H: " & MyTimeSpan.Hours & " M: " & MyTimeSpan.Minutes)

Of course these examples really have no error handling. You mentioned you use a masked edit textbox, but you still may want to employ additional error handling incase any of the strings used as input are not valid date/times.





Re: Visual Basic Language How To Sum Time...?

eldoktor007

Code Snippet

Dim StartTime As DateTime = DateTime.Parse(Me.MaskedTextBox2.Text) '"11:00 AM"

Dim EndTime As DateTime = DateTime.Parse(Me.MaskedTextBox1.Text) '"1:30 PM"

Dim TimeDifferent As TimeSpan = EndTime.Subtract(StartTime)

Code Snippet

MessageBox.Show(TimeDifferent.ToString())

this code give me result

9:30:00

but in the true if i i will say ......

1:30 PM

-

11:00 AM

============

result must be 2:30

count ...

12:00

01:30

result must be 2:30 Hours

this result i want it .

your code don't know about AM Or PM

Fix It Please

and

thanx for Help






Re: Visual Basic Language How To Sum Time...?

Brendan Grant

What culture/language is your computer running

When I run the following:

Code Snippet

Dim StartTime As DateTime = DateTime.Parse("11:00 AM")

Dim EndTime As DateTime = DateTime.Parse("1:30 PM")

Dim TimeDifferent As TimeSpan = EndTime.Subtract(StartTime)

MessageBox.Show(TimeDifferent.ToString())

I get "02:30:00" as I would expect.






Re: Visual Basic Language How To Sum Time...?

rkimble

I agree that the posted code should and does work. I notice that you've got your TextBox1 and TextBox2 are backward from Matt's example, but that should just result in a negative value (-2:30:00). There must be a regional setting issue (or more to the code you tried than we are seeing).

I believe Brendan and Matt have you on the correct track. I would do the subtraction just like Matt, but using TryParse() instead of Parse(). The list addition I would do similarly, but with just a single incrementing TimeSpan. Here is example code for a Form with 2 TextBoxes, a Label1, a Button1, then a ListBox, with a Label2 and Button2:

Code Snippet

Public Class Form1

'Function to subtract 2 times, with error checking

Private Function TryAddTimes(ByVal time1 As String, ByVal time2 As String) As String

'Variables to hold start and end times

Dim dt1, dt2 As DateTime

'Try to parse text 1 into a time value

If Not DateTime.TryParse(time1, dt1) Then

Return "Invalid Time 1"

End If

'Try to parse text 2 into a time value

If Not DateTime.TryParse(time2, dt2) Then

Return "Invalid Date 1"

End If

'Create the timespan variable

Dim dif As TimeSpan

'Subtract the times

dif = dt2.Subtract(dt1)

'Return the string representation

Return dif.ToString()

End Function

'Function to sum time list of times

Private Function TrySumTimeList(ByVal lst As ListBox) As String

'Variable to hold the total time

Dim sum As New TimeSpan(0)

'Flag to mark if a list item fails to convert

Dim err As Boolean = False

'Try to add each list item

For Each o As Object In lst.Items

'Variable to hold converted list item value

Dim ts As TimeSpan

'Try to convert list item

If TimeSpan.TryParse(o.ToString, ts) Then

'On success, add to total

sum = sum.Add(ts)

Else

'On failure set error flag

err = True

End If

Next

'Variable for return value

Dim rtn As String

'Get total time as string

rtn = sum.ToString()

'Add error message if error occured

If err Then

rtn &= " (some times could not be parsed)"

End If

'Return total time string, with error msg (if any)

Return rtn

End Function

'Test time subtraction

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Label1.Text = Me.TryAddTimes(Me.TextBox1.Text, Me.TextBox2.Text)

End Sub

'Test time addition

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.Label2.Text = Me.TrySumTimeList(Me.ListBox1)

End Sub

End Class

Any and all of the examples given should work. TaDa's code appears functional but does contain some extra method calls that make these other approaches slightly (and very slightly) more efficient.






Re: Visual Basic Language How To Sum Time...?

Spidermans_DarkSide - MSP, VSIP

Hi,

This code shows AM and PM being used.

Just add 1 BUTTON to a FORM to try this.>>>>

Code Snippet

Public Class Form1

'The next highlighted line of code should be one line in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

My.Application.ChangeCulture("en-US")

Dim dateTime1 As Date = CDate("08/23/2007 11:00:00AM")

Dim dateTime2 As Date = CDate("08/23/2007 01:30:00PM")

MessageBox.Show(dateTime2.Subtract(dateTime1).ToString)

End Sub

End Class

Regards,

S_DS






Re: Visual Basic Language How To Sum Time...?

eldoktor007

Was this post helpful

Code Snippet

Dim StartTime As DateTime = DateTime.Parse("11:00 AM")

Dim EndTime As DateTime = DateTime.Parse("1:30 PM")

Dim TimeDifferent As TimeSpan = EndTime.Subtract(StartTime)

MessageBox.Show(TimeDifferent.ToString())

Now is Working

thanx Alot man

and thanx for any body try to Help me






Re: Visual Basic Language How To Sum Time...?

Brendan Grant

Glad we could help. Would you mind marking those threads you found helpful as such and mark your answer