如何从字符串VB转换日期时间

时间:2014-01-20 08:18:20

标签: vb.net string date datetime converter

有谁知道如何将以下字符串转换为VB中的DateTime值? 如何将字符串转换为datetime? 我的代码在下面提供,非常感谢一些帮助。

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    conn = New SqlConnection(connectionstring)
    conn.Open()

    Dim combo1 As Integer
    combo1.ToString("yyyy-MM-dd HH:mm:ss")
    combo1 = ComboBox1.SelectedIndex
    asd = New SqlCommand("select jam from jadwal where waktu_booking = '" & combo1 & "'", conn)
    TextBox7.Text = asd.ExecuteScalar()
    adm = New SqlCommand("select tarif from tarif_sewa where kode_tarif = '" & TextBox7.Text & "'", conn)
    TextBox2.Text = adm.ExecuteScalar()
    conn.Close()
End Sub

我收到错误消息“从字符串转换datetime时转换失败。”在TextBox7.Text = asd.ExecuteScalar()

1 个答案:

答案 0 :(得分:0)

使用date.tryparse。
例如)

Dim datstring As String = "2008-01-21 09:10:01"
Dim dateValue As Date
If Date.TryParse(DateString, dateValue) Then
    MsgBox(dateValue)
End If