Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't see the wood for all the trees now....
I am work on a time program, for staff to check in and out.
in a grid i show all the days working and add the total hours work for that day(GetTotalTime). that means during the grid Initialize Row i use this:

VB
Private Sub GrdOverViewInitializeRow(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeRowEventArgs) Handles grdOverView.InitializeRow

'Calculate daily hours and enter this in cell 9

         StartTime = e.Row.Cells(4).Text.ToString
         StartDate = e.Row.Cells(5).Text.ToString
         FinishTime = e.Row.Cells(6).Text.ToString
         FinishDate = e.Row.Cells(7).Text.ToString

         StartTime = String.Format("{0} {1}", StartDate, StartTime)
         FinishTime = String.Format("{0} {1}", FinishDate, FinishTime)

         WorkTime = GetTotalTime(StartTime, FinishTime)
         grdOverView.Text = CalculateTime(WorkTime)
         e.Row.Cells(9).Value = WorkTime

End Sub


So fare so good, however during this i use Calculatetime as below for Grand total:

VB
Friend Function CalculateTime(ByVal calworktime As String) As String

         Try

            Dim input As String = calworktime
            Dim parts = input.Split(":"c)
            Dim hours = Int32.Parse(parts(0))
            Dim minutes = Int32.Parse(parts(1))
            Dim result = New TimeSpan(hours, minutes, 0) 
            TotalTime = TotalTime.Add(result)

            Dim s As String = String.Format("{0}:{1}", TotalTime.TotalHours.ToString("00"), TotalTime.Minutes.ToString("00"))

            CalculateTime = String.Format("Total time: {0}", s.ToString)
            Return CalculateTime

         Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
            Return Nothing
         End Try

End Function


This is where it goes wrong. if it is just adding whole hours together, no problems.
howere for exsample: 5 hrs + 45 min=6hrs and 45min...where did the extra hour come from?????

WorkTime and TotalTime is declare in a general module.

Any ideas where i am losing the plot...
Thnaks
Posted
Updated 25-Oct-11 8:10am
v3

Rule number one of doing any form of arithmetic: do not re-invent the wheel.

The .NET framework includes two classes specifically for dealing with dates and times: DateTime and TimeSpan. Instead of using strings for arithmetic, use a combination of these two classes. They make life so much easier...
 
Share this answer
 
Comments
Sander Rossel 25-Oct-11 14:11pm    
To often have I seen stuff like that. Converting dates to strings to China and back again... :(
My 5 for the answer!
VB Overlord 25-Oct-11 15:59pm    
well, any ideas why "CalculateTime", give me a extra hour?
Also the format of hours used have to be in HH:mm, since the is no point in giving for exsample 4 days 12 hours 23 min on a time sheet..

the code in "grid Initialize Row" work fine and do calculate right when i test with hours over 24. seems to me that the problem with timespan starts when you hit more that 24 hrs.
Sander Rossel 25-Oct-11 16:13pm    
You are replying to the wrong person. I replied to OriginalGriff. As he is the one who attempted to answer your question I suggest you reply to him too :)
Anyway, where does that TotalTime come from? Perhaps it already has an initial value of 1...
VB Overlord 26-Oct-11 4:12am    
Sorry mate.
Anyway totaltime is defined in a "General Module"
Thank anyway
This seems to be working:
Reason this is a string return, is that is it used at the bottom of the payslip
which means it have to show "HH:mm" format

however anybody got a fast or more better way, please let me know.

Friend Function CalculateTime(ByVal calworktime As String) As String

Try

Dim input As String = calworktime
Dim parts = input.Split(":"c)
Dim hours = Int32.Parse(parts(0))
Dim minutes = Int32.Parse(parts(1))
Dim result = New TimeSpan(hours, minutes, 0)

TotalTime = TotalTime.Add(result)

CalculateTime = String.Format("Ialt timer brugt: {0}{1}", Format((TotalTime.Days * 24) + TotalTime.Hours, "#00"), Format(TotalTime.Minutes, ":#00"))

Return CalculateTime

Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Return Nothing
End Try

End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900