-2

Im using below code which give me these results:

2.12:00:00

1.05:55:14.4679488

How to format the string that will give me a result in 24hours format like these:

60:00:00

29:55:14

Thanks for your help. Im using vb.net 2008.

    Dim TimeIn As New System.DateTime
    Dim TimeOut As New System.DateTime

    For x As Integer = 0 To RecordsDataGridview.Rows.Count - 2

        Dim TimeInvalue As String = RecordsDataGridview.Rows(x).Cells(10).Value.ToString()

        If Not IsDBNull(RecordsDataGridview.Rows(x).Cells(10).Value) AndAlso RecordsDataGridview.Rows(x).Cells(10).Value.ToString.Length <> 0 Then
            TimeIn = DateTime.Parse(TimeInvalue)
        End If

        Dim TimeOutvalue As String = RecordsDataGridview.Rows(x).Cells(11).Value.ToString()

        If Not IsDBNull(RecordsDataGridview.Rows(x).Cells(11).Value) AndAlso RecordsDataGridview.Rows(x).Cells(11).Value.ToString.Length <> 0 Then
            TimeOut = DateTime.Parse(TimeOutvalue)
        End If

        If IsDBNull(RecordsDataGridview.Rows(x).Cells(10).Value()) OrElse RecordsDataGridview.Rows(x).Cells(10).Value() Is Nothing OrElse RecordsDataGridview.Rows(x).Cells(10).Value.ToString.Trim() = "" Then
            RecordsDataGridview.Rows(x).Cells(12).Value() = Nothing
        End If

        If IsDBNull(RecordsDataGridview.Rows(x).Cells(11).Value()) OrElse RecordsDataGridview.Rows(x).Cells(11).Value() Is Nothing OrElse RecordsDataGridview.Rows(x).Cells(11).Value.ToString.Trim() = "" Then
            Dim StayingHours2 As TimeSpan = (DateTime.Now - TimeIn)

            String.Format("{0:00}:{1:00}:{2:00}", StayingHours2.TotalHours, StayingHours2.Minutes, StayingHours2.Seconds)

            RecordsDataGridview.Rows(x).Cells(12).Value() = StayingHours2

        Else
            Dim StayingHours1 As TimeSpan = (TimeOut - TimeIn)

            String.Format("{0:00}:{1:00}:{2:00}", StayingHours1.TotalHours, StayingHours1.Minutes, StayingHours1.Seconds)

            RecordsDataGridview.Rows(x).Cells(12).Value() = StayingHours1

        End If

    Next
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
phil_19
  • 105
  • 1
  • 9
  • how is the code not doing what you want? – Ňɏssa Pøngjǣrdenlarp Nov 15 '14 at 18:11
  • I don't know the data type of the cell but you are not assigning the String.Format to anything. – dbasnett Nov 15 '14 at 18:18
  • I want the output to be in 24hours format. If there's something wrong with my code, please can you tell me how to code to get 24hours format? I'm new to vb.net. Thanks! – phil_19 Nov 15 '14 at 18:41
  • 1
    possible duplicate of [Format TimeSpan greater than 24 hour](http://stackoverflow.com/questions/3505230/format-timespan-greater-than-24-hour) – Joe Nov 15 '14 at 19:48

1 Answers1

1

It's working now guys. Thanks for the suggestions!

I use below code:

Dim TimeIn As New System.DateTime
Dim TimeOut As New System.DateTime

For x As Integer = 0 To RecordsDataGridview.Rows.Count - 2

    Dim TimeInvalue As String = RecordsDataGridview.Rows(x).Cells(10).Value.ToString()

    If Not IsDBNull(RecordsDataGridview.Rows(x).Cells(10).Value) AndAlso RecordsDataGridview.Rows(x).Cells(10).Value.ToString.Length <> 0 Then
        TimeIn = DateTime.Parse(TimeInvalue)
    End If

    Dim TimeOutvalue As String = RecordsDataGridview.Rows(x).Cells(11).Value.ToString()

    If Not IsDBNull(RecordsDataGridview.Rows(x).Cells(11).Value) AndAlso RecordsDataGridview.Rows(x).Cells(11).Value.ToString.Length <> 0 Then
        TimeOut = DateTime.Parse(TimeOutvalue)
    End If

    If IsDBNull(RecordsDataGridview.Rows(x).Cells(10).Value()) OrElse RecordsDataGridview.Rows(x).Cells(10).Value() Is Nothing OrElse RecordsDataGridview.Rows(x).Cells(10).Value.ToString.Trim() = "" Then
        RecordsDataGridview.Rows(x).Cells(12).Value() = Nothing
    End If

    If IsDBNull(RecordsDataGridview.Rows(x).Cells(11).Value()) OrElse RecordsDataGridview.Rows(x).Cells(11).Value() Is Nothing OrElse RecordsDataGridview.Rows(x).Cells(11).Value.ToString.Trim() = "" Then
        Dim StayingHours2 As TimeSpan = (DateTime.Now - TimeIn)

    Dim StayingHours2Result As String = String.Format("{0:00}:{1:00}:{2:00}", StayingHours2.TotalHours, StayingHours2.Minutes, StayingHours2.Seconds)

        RecordsDataGridview.Rows(x).Cells(12).Value() = StayingHours2Result

    Else
        Dim StayingHours1 As TimeSpan = (TimeOut - TimeIn)

    Dim StayingHours1Result As String = String.Format("{0:00}:{1:00}:{2:00}", StayingHours1.TotalHours, StayingHours1.Minutes, StayingHours1.Seconds)

        RecordsDataGridview.Rows(x).Cells(12).Value() = StayingHours1Result

    End If

Next
phil_19
  • 105
  • 1
  • 9