vb.net 将 Double 转换为时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15635254/
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
Converting Double to Time Format
提问by AltF4_
I have a bunch of numbers which I get which are like 1.567or 22.654or 220.123and I want to convert them to time in the 00:00:00.000 (hours/Minutes/Seconds.Miliseconds)
我有一堆数字,它们是像1.567或22.654或这样的220.123,我想将它们转换为时间00:00:00.000 (hours/Minutes/Seconds.Miliseconds)
Any advice on how I can achieve this efficently ?? without too much fiddling.
关于如何有效地实现这一目标的任何建议?没有太多的摆弄。
I need a vb.net solution where possible.
在可能的情况下,我需要一个 vb.net 解决方案。
Thanks for the help in advance.
我在这里先向您的帮助表示感谢。
回答by MarcinJuraszek
Use DateTime.AddHoursmethod. It takes doublevalues as a parameter:
使用DateTime.AddHours方法。它以double值作为参数:
Dim d as DateTime = (new DateTime()).AddHours(1.567)
Result is {0001-01-01 01:34:01}
结果是 {0001-01-01 01:34:01}
Then you can use DateTime.ToString()method to get time in desired format:
然后您可以使用DateTime.ToString()方法以所需格式获取时间:
Dim s as String = d.ToString("HH:mm:ss.fff")
Results is "01:34:01.200"
结果是 "01:34:01.200"
It will work for all values from 0to 23.99
它将适用于从0到的所有值23.99
UPDATE
更新
If your input values represents hours and they can be greater then 24 you can use TimeSpaninstead of DateTime:
如果你的输入值代表小时,他们可以大于24就可以使用TimeSpan,而不是DateTime:
Dim d as TimeSpan = TimeSpan.FromHours(220.123)
Dim s As String = string.Format("{0}:{1}:{2}.{3}", CInt(d.TotalHours), d.Minutes, d.Seconds, d.Milliseconds)

