VB.NET 如何在列表框中获取总小时数和分钟数
VB.NET How to get total hours and minutes in Listbox
请帮忙!我需要从 ListBox 中获取格式为 "HH:mm" 的总小时数和分钟数,例如:
11:20
22:40
34:00
总计:68:00
我尝试使用 Datetime 和 TimeSpan,但出现错误:
"The DateTime represented by the string is not supported in calendar
System.Globalization.GregorianCalendar."
这是我的代码:
ListBox_monthtime.Items.Add("11:20")
ListBox_monthtime.Items.Add("22:40")
ListBox_monthtime.Items.Add("34:00")
'SUM TIMES IN LISTBOX
Dim MyDateTimeMonthly As DateTime
Dim MyTimeSpanMonthly As New TimeSpan
For Each S As String In ListBox_monthtime.Items
MyDateTimeMonthly = DateTime.ParseExact(S, "HH:mm", System.Globalization.CultureInfo.InvariantCulture)
MyTimeSpanMonthly = MyTimeSpanMonthly.Add(New TimeSpan(MyDateTimeMonthly.Day, MyDateTimeMonthly.Hour, MyDateTimeMonthly.Minute, 0))
Next
monthtime_txt.Text = (MyTimeSpanMonthly.Days * 24 + MyTimeSpanMonthly.Hours) & ":" & MyTimeSpanMonthly.Minutes
也许这会有所帮助:
ListBox_monthtime.Items.Add("11:43")
ListBox_monthtime.Items.Add("22:56")
ListBox_monthtime.Items.Add("34:21")
Dim totalHours As Integer
Dim totalMinutes As Integer
For Each S As String In ListBox_monthtime.Items
totalHours += S.Split(":")(0)
totalMinutes += S.Split(":")(1)
Next
Dim remainder = totalMinutes Mod 60
totalHours += totalMinutes / 60
Dim totalTime = totalHours & ":" & remainder.ToString("D2")
monthtime_txt.Text = totalTime
虽然你仍然会转换字符串-整数,所以我会把它放在 Try/Catch
您不能使用大于 24 的小时值从字符串创建 DateTime 或 Timespan。您需要自己解析输入并将其转换为有效字符串以供 TimeSpan.parse()
使用.
Dim TotalTime As TimeSpan = TimeSpan.Zero
For Each item As String In ListBox_monthtime.Items
TotalTime = TotalTime.Add(TimeSpan.Parse(FormatTimeString(item)))
Next
Me.monthtime_txt.Text = $"{CInt(TotalTime.TotalHours).ToString}:{TotalTime.Minutes}"
Private Function FormatTimeString(TimeString As String) As String
Dim timeArr() As String = TimeString.Split(":")
If CInt(timeArr(0)) > 24I Then
Dim d As Int32 = CInt(timeArr(0)) \ 24I
FormatTimeString = $"{d}:{(CInt(timeArr(0)) - (24I * d)).ToString}:{timeArr(1)}:00"
Else
FormatTimeString = TimeString
End If
End Function
请帮忙!我需要从 ListBox 中获取格式为 "HH:mm" 的总小时数和分钟数,例如:
11:20
22:40
34:00
总计:68:00
我尝试使用 Datetime 和 TimeSpan,但出现错误:
"The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."
这是我的代码:
ListBox_monthtime.Items.Add("11:20")
ListBox_monthtime.Items.Add("22:40")
ListBox_monthtime.Items.Add("34:00")
'SUM TIMES IN LISTBOX
Dim MyDateTimeMonthly As DateTime
Dim MyTimeSpanMonthly As New TimeSpan
For Each S As String In ListBox_monthtime.Items
MyDateTimeMonthly = DateTime.ParseExact(S, "HH:mm", System.Globalization.CultureInfo.InvariantCulture)
MyTimeSpanMonthly = MyTimeSpanMonthly.Add(New TimeSpan(MyDateTimeMonthly.Day, MyDateTimeMonthly.Hour, MyDateTimeMonthly.Minute, 0))
Next
monthtime_txt.Text = (MyTimeSpanMonthly.Days * 24 + MyTimeSpanMonthly.Hours) & ":" & MyTimeSpanMonthly.Minutes
也许这会有所帮助:
ListBox_monthtime.Items.Add("11:43")
ListBox_monthtime.Items.Add("22:56")
ListBox_monthtime.Items.Add("34:21")
Dim totalHours As Integer
Dim totalMinutes As Integer
For Each S As String In ListBox_monthtime.Items
totalHours += S.Split(":")(0)
totalMinutes += S.Split(":")(1)
Next
Dim remainder = totalMinutes Mod 60
totalHours += totalMinutes / 60
Dim totalTime = totalHours & ":" & remainder.ToString("D2")
monthtime_txt.Text = totalTime
虽然你仍然会转换字符串-整数,所以我会把它放在 Try/Catch
您不能使用大于 24 的小时值从字符串创建 DateTime 或 Timespan。您需要自己解析输入并将其转换为有效字符串以供 TimeSpan.parse()
使用.
Dim TotalTime As TimeSpan = TimeSpan.Zero
For Each item As String In ListBox_monthtime.Items
TotalTime = TotalTime.Add(TimeSpan.Parse(FormatTimeString(item)))
Next
Me.monthtime_txt.Text = $"{CInt(TotalTime.TotalHours).ToString}:{TotalTime.Minutes}"
Private Function FormatTimeString(TimeString As String) As String
Dim timeArr() As String = TimeString.Split(":")
If CInt(timeArr(0)) > 24I Then
Dim d As Int32 = CInt(timeArr(0)) \ 24I
FormatTimeString = $"{d}:{(CInt(timeArr(0)) - (24I * d)).ToString}:{timeArr(1)}:00"
Else
FormatTimeString = TimeString
End If
End Function