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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21665354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:43:55  来源:igfitidea点击:

How to convert double to time in vb.net

vb.netdatetimedoubletype-conversion

提问by user3275784

I have a variable that stores the time in entered by a user. I get an error that says it cannot convert it from a double to a date. How do I go about doing this?

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

Essentially I need to take the time in from a user and convert it into minutes. I'm not sure how to do this though.

本质上,我需要从用户那里获取时间并将其转换为分钟。我不知道如何做到这一点。

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

回答by Gabriel De Oliveira

Dim myDateTime As DateTime = DateTime.FromOADate(myDouble)

回答by JaredPar

The Valfunction converts, in this particular overload, a Stringvalue to a Double. This type is not convertible to DateTimeand hence the compiler issues an error.

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

In order to convert a Stringto a date use Datetime.Parseinstead

为了将 a 转换String为日期,请Datetime.Parse改用

m_decTimeIn = DateTime.Parse(txtTimeIn.Text)