将分钟转换为小时:,在vb.net中分钟

时间:2017-04-12 21:20:59

标签: vb.net

我想将总分钟转换为小时和分钟hh:mm ..

这是我的代码

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
    Dim TotalMinute As Int32

    Dim Minute As Int32
    Dim Hour As Int32

    Try
        TotalMinute = CType(TextBox9.Text, Int32)

        TotalMinute = TotalMinute Mod 1440

        Hour = TotalMinute \ 60
        Minute = TotalMinute Mod 60

        TextBox5.Text = FormatTwoDigits(Hour) & ":" & FormatTwoDigits(Minute)

    Catch ex As Exception
        Exit Sub
    End Try

End Sub

Private Function FormatTwoDigits(ByVal i As Int32) As String
    If 30 > i Then
        FormatTwoDigits = "0" & i.ToString
    Else
        FormatTwoDigits = i.ToString
    End If
End Function

此代码在24小时内正常运行..但是在24 ...

之后从0开始计算

例如,如果输入是1500分钟,则应该说25:00而不是01:00

3 个答案:

答案 0 :(得分:3)

删除您的TotalMinute = TotalMinute Mod 1440行,如果总分钟超过一天,则表示要删除任何“日”部分。 (60分钟= 1小时,24小时= 1天,因此1440分钟= 1天。)

答案 1 :(得分:1)

或者你可以让.Net完成这项工作

    Dim ts As TimeSpan = TimeSpan.FromMinutes(TotalMinute)

    Dim s As String = String.Format("{0:00}:{1:00}", Math.Floor(ts.TotalHours), ts.Minutes)

答案 2 :(得分:0)

我做的更像是:

Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
    Dim TotalMinute As Integer
    If Integer.TryParse(TextBox9.Text, TotalMinute) Then
        Dim ts As TimeSpan = TimeSpan.FromMinutes(TotalMinute)
        TextBox5.Text = CType(ts.TotalHours, Integer).ToString("00") & ":" & ts.Minutes.ToString("00")
    End If
End Sub