vb.net 将总分钟数转换为 HH:mm 格式的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4135775/
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
Best way to convert total minutes into HH:mm format?
提问by Neal
I get a return value from a web service in minutes, for example 538 minutes. I need to break this down in hours and minutes. What is the fastest way, in .net code and also VB6 code (two apps use the service) to convert this from minutes to HH:mm?
我在几分钟内从 Web 服务获得一个返回值,例如 538 分钟。我需要在几小时和几分钟内分解它。在 .net 代码和 VB6 代码(两个应用程序使用该服务)中将其从分钟转换为 HH:mm 的最快方法是什么?
Thanks
谢谢
回答by Will Marcouiller
This code should work both in .NET and VB6:
这段代码应该在 .NET 和 VB6 中都可以工作:
Dim hours As Integer = 538 \ 60
Dim minutes As Integer = 538 - (hours * 60)
Dim timeElapsed As String = CType(hours, String) & ":" & CType(minutes, String)
label1.Text = timeElapsed
In .NET exclusively, you should be able to do the following (which requires to be tested):
仅在 .NET 中,您应该能够执行以下操作(需要进行测试):
Dim timeElapsed As DateTime = New DateTime(1, 1, 1, 0, 538, 0)
label1.Text = timeElapsed.ToString("HH:mm")
I hope this helps!
我希望这有帮助!
回答by MarkJ
In VB6 you could just use Format(538/1440.0, "hh:mm")
在VB6中你可以使用 Format(538/1440.0, "hh:mm")
VB6 Date
values can be treated as a number of days, and there's 1440 minutes in a day. So 538/1440 is the number of days in your period, and then you can use Format
VB6Date
值可以视为天数,一天有 1440 分钟。所以538/1440就是你经期的天数,然后你就可以用Format
回答by DJIDave
In .Net you have a TimeSpan class so you can do the following
在 .Net 中,您有一个 TimeSpan 类,因此您可以执行以下操作
Dim t As New TimeSpan(0, 538, 0)
'Then you have the 2 properties
t.Hours
t.Minutes
回答by Matthew Flaschen
Take modulus 60, then integer-divide by 60.
取模数 60,然后整数除以 60。
In VB, integer division uses \
.
在 VB 中,整数除法使用\
.
回答by Geoffrey
If you need to perform operations with other DateTime objects it might be useful to use a TimeSpan object instead, e.g.
如果您需要对其他 DateTime 对象执行操作,则改用 TimeSpan 对象可能会很有用,例如
Dim oTS As New TimeSpan(0, 538, 0)
MessageBox.Show(Format(oTS.Hours, "00") & ":" & Format(oTS.Minutes, "00"))
Dim startime As DateTime = Date.Now
Dim newtime As DateTime = startime + oTS
MessageBox.Show(newtime.ToString("HH:mm"))
If not then Matthew's suggestion of using Integer division '\' and modulo 'Mod' will work very well.
如果不是,那么马修关于使用整数除法 '\' 和模数 'Mod' 的建议将非常有效。