小时格式使用Timespan超过24小时

时间:2014-11-15 18:04:28

标签: vb.net datagridview

我使用下面的代码给出了这些结果:

2.12:00:00

1.05:55:14.4679488

如何格式化将以24小时格式给出结果的字符串,如下所示:

60:00:00

29:55:14

感谢您的帮助。我正在使用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

1 个答案:

答案 0 :(得分:1)

现在正在工作的人。谢谢你的建议!

我使用下面的代码:

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
相关问题