Here is a VB 6 fuction I used to calculate number of hours worked given a start time and an end time (24 hour format). This function worked even when start time > end time (17:00 start time - 03:12 end time next morning). The result is formatted in tenths of an hour (ex. 10.2 hours worked).

I'm trying to update this function to work with MS Visual Studio 2010.

Any help would be greatly appreciated. Thanks.

Function CalculateDailyHours(InTime As String, OutTime As String) As String
Dim StartTime As Date
Dim EndTime As Date
Dim Temp As Date
Dim Minutes As Integer
' **************************************
StartTime = CDate(InTime)
EndTime = CDate(OutTime)
' **************************************
EndTime = DateAdd("d", 1, EndTime) ' Initialize a date
If StartTime > EndTime Then
EndTime = DateAdd("d", 1, EndTime) ' Add 24 hours if ST>ET
Temp = EndTime - Startime
Else
Temp = EndTime - StartTime
End If
' *************************************
Minutes = DatePart("n", Temp) * (5 / 3) ' Convert minutes to fraction
' *************************************
CalculateDailyHours = Format$(DatePart("h", Temp) + (Minutes / 100), "##.00")
End Function

Your codes are quite right for vb6, but not in vb.net. Some little modification is needed. The codes that should help you are

Function CalculateDailyHours(InTime As String, OutTime As String) As String
        Dim StartTime As Date
        Dim EndTime As Date
        Dim Temp As TimeSpan

        ' **************************************
        StartTime = CDate(InTime)
        EndTime = CDate(OutTime)
        ' **************************************

        If StartTime > EndTime Then
            EndTime = DateAdd("d", 1, EndTime) ' Add 24 hours if ST>ET
            Temp = EndTime - StartTime
        Else
            Temp = EndTime - StartTime
        End If

        Return Format(Temp.TotalHours(), "00.00")
    End Function

If you're going to point out the difference between VB6 and VB.Net, it may be useful to provide some useful methods provided by .Net. This simple function using Date,Time and TimeSpan will return the hours worked and minutes in 1\10 intervals.

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

        Console.WriteLine(CalculateDailyHours("29/10/2014 17:00", "30/10/2014 03:12"))

    End Sub

    'Make the returned value a double so it can easily be used for future calculations
    Private Function CalculateDailyHours(StartValue As String, EndValue As String) As Double

        Dim StartTime As DateTime = BuildDateTime(StartValue)
        Dim EndTime As DateTime = BuildDateTime(EndValue)

        Dim Diff As TimeSpan = EndTime - StartTime

        'Get The Hour Difference and The Minute Difference in intervals of 10%
        Return Diff.Hours & "." & CInt((10 / 60) * Diff.Minutes)

    End Function

    Private Function BuildDateTime(DTValue As String) As DateTime

        Dim vDate As Date = DTValue

        Return New DateTime(vDate.Year, vDate.Month, vDate.Day, vDate.Hour, vDate.Minute, 0)

    End Function

The returned value is 10.2 as a double

commented: Good! +15

Note: The calculation at line 16 rounds to the nearest integer (up or down). assuming you are going to allow rounding up (Not very business minded I know), you may want to replace line 16 with the following code

'If the Minute value is > 95% then increase the hour by 1
If CInt((10 / 60) * Diff.Minutes) > 9 Then
    Return Diff.Hours + 1 
Else
    Return Diff.Hours & "." & CInt((10 / 60) * Diff.Minutes)
End If

Oherwise if you are business minded replace line 16 with the following to always ROUND DOWN to the nearest 10%

Return Diff.Hours & "." & (CInt((100 / 60) * Diff.Minutes)) \ 10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.