如何在vb.net中将double转换为时间

时间:2014-02-09 21:34:05

标签: vb.net datetime double type-conversion

我有一个变量,用于存储用户输入的时间。我收到一个错误,说它无法将它从双精度转换为日期。我该怎么做呢?

基本上我需要从用户那里抽出时间并将其转换为分钟。我不知道怎么做。

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtTimeIn.TextChanged, txtTimeIn.TextChanged

    Dim m_decTimeIn As DateTime


    m_decTimeIn = Val(txtTimeIn)

End Sub

2 个答案:

答案 0 :(得分:5)

Dim myDateTime As DateTime = DateTime.FromOADate(myDouble)

答案 1 :(得分:0)

Val函数在此特定重载中将String值转换为Double。此类型不能转换为DateTime,因此编译器会发出错误。

要将String转换为日期而使用Datetime.Parse而不是

m_decTimeIn = DateTime.Parse(txtTimeIn.Text)