如何将总时间转换为分钟

时间:2017-02-19 14:50:08

标签: vb.net

This was the error msg im getting enter image description here我的数据库有一行“Total_Time”(时间)。 它的格式为HH:MM:SS。我需要代码将“Total_time”转换为分钟。

例如,如果Total_time = 01:30:00,则答案应为Total_minutes = 90, 我想将total_minutes乘以“Other”(int变量)。

以下是我的尝试:

  Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


    con = New System.Data.SqlClient.SqlConnection
    Try


        con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
        con.Open()


        Dim cm As SqlClient.SqlCommand
        cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=@id", con)
        cm.Parameters.AddWithValue("@id", TextBox5.Text)
        dr = cm.ExecuteReader()
        While dr.Read()
            Dim tt As Double
            tt = dr("Total_Time").ToString
            Dim other As Double
            other = dr("Other").ToString

            Dim str() As String
            Dim strmin As Double
            str = Split(tt.ToString, ":")
            strmin = (CDbl(str(1)) * 60 + CDbl(str(2)) + CDbl(str(3)) / 60).ToString

            Dim total As Decimal
            total = strmin + other
            Label7.Text = total.ToString
        End While
    Catch ex As Exception

    End Try
End Sub

但是当我点击时没有发生任何事情,label7没有显示任何值 提前谢谢。

3 个答案:

答案 0 :(得分:1)

Dim Total_minutes As Double = CDate("1:23:45").TimeOfDay.TotalMinutes          ' 83.75

为避免类似错误,我强烈建议您使用Option Strict

Dim Total_Time As DateTime = Convert.ToDateTime(dr!Total_Time)
Dim Total_minutes# = Total_Time.TimeOfDay.TotalMinutes

Dim Other# = Val(dr!Other)
Dim total# = Total_minutes * Other

Label7.Text = total.ToString

答案 1 :(得分:0)

尝试以下

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    con = New System.Data.SqlClient.SqlConnection
    Try
        con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
        con.Open()

        Dim cm As SqlClient.SqlCommand
        cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=@id", con)
        cm.Parameters.AddWithValue("@id", TextBox5.Text)
        dr = cm.ExecuteReader()
        While dr.Read()     
            Dim other As TimeSpan           
            Dim tt As TimeSpan

            If TimeSpan.TryParse(dr("Total_Time"), tt) Then
                If TimeSpan.TryParse(dr("Other"), other) Then
                    tt = tt.Add(other)
                Else
                    'Do something like show error message for incorrect data for dr("Other")
                End If
                Label7.Text = tt.TotalMinutes.ToString
            Else
                'Do something like show error message for incorrect data for dr("Total_Time")
            End If
        End While
    Catch ex As Exception

    End Try
End Sub

如果时间超过24:00:00,请使用以下代码

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    con = New System.Data.SqlClient.SqlConnection
    Try
        con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
        con.Open()

        Dim cm As SqlClient.SqlCommand
        cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=@id", con)
        cm.Parameters.AddWithValue("@id", TextBox5.Text)
        dr = cm.ExecuteReader()
        While dr.Read()     
            Try
                Dim dataTime As String = dr("Total_Time").ToString
                dataTime = dataTime.Split("."c)(0).ToString
                Dim tt As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
                dataTime = dr("Other").ToString
                dataTime = dataTime.Split("."c)(0).ToString
                Dim other As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
                tt = tt.Add(other)
                dataTime = tt.TotalMinutes.ToString

            Catch ex As Exception
                'do something here as string is not a time
            End Try
        End While
    Catch ex As Exception

    End Try
End Sub

答案 2 :(得分:0)

不要一直切换到字符串......并使用Timespan

如果你的标签永远不会改变,那么例程必须是错误输出,检查catch语句中的错误。但是从你的形象看,其他没有价值。

使用数据库时,你应该总是测试dbnull。 DBNull在数学和布尔比较中有一些奇怪的行为。

用这个

替换你的while循环
    If Not Dr.read OrElse IsDBNull(Dr("Total_time")) OrElse IsDBNull(Dr("Other")) Then
        Label7.text = "ERR"
    Else
        Dim ts As TimeSpan = TimeSpan.Parse(dr("Total_time").ToString)
        Label7.text = (ts.TotalMinutes * Dr("Other")).ToString
    End If

PS:你的问题说再乘以其他,但你的表格/代码说添加......我回答了问题。