拆分并计算字符串 vb.net
Split and calculate string vb.net
我有一个很难的问题。
我有一个像
这样的字符串
2021-09-24 00:52:27
2021-09-23 03:46:08
2021-09-22 04:13:03
2021-09-21 04:08:02
2021-09-20 02:13:27
2021-09-19 05:27:38
2021-09-18 03:43:32
2021-09-17 03:23:44
我需要计算上周的时间跨度。
字符串并不总是最后 7 天
所以我做了一个字符串来获取最近 7 天并比较它们,如果一行与日期不匹配,它将是 00:00:00,
在那之前我把绳子分开了。
在此之前,我将 X 声明为字符串中的多行
2021-09-24
2021-09-23
2021-09-22
2021-09-21
2021-09-20
2021-09-19
2021-09-18
2021-09-17
00:52:27
03:46:08
04:13:03
04:08:02
02:13:27
05:27:38
03:43:32
03:23:44
Dim x As String = RichTextBox4.Lines.Count ' The number of lines with date and hours together
Dim a1 As String = Date.Now.ToString("yyyy-MM") ' Creating dates for the last 7 days
Dim a2 As String = Date.Now.ToString("dd")
Dim b1 As String = Date.Now.ToString("yyyy-MM")
Dim b2 As String = Date.Now.ToString("dd") - 1
Dim c1 As String = Date.Now.ToString("yyyy-MM")
Dim c2 As String = Date.Now.ToString("dd") - 2
Dim dd1 As String = Date.Now.ToString("yyyy-MM")
Dim dd2 As String = Date.Now.ToString("dd") - 3
Dim e1 As String = Date.Now.ToString("yyyy-MM")
Dim e2 As String = Date.Now.ToString("dd") - 4
Dim f1 As String = Date.Now.ToString("yyyy-MM")
Dim f2 As String = Date.Now.ToString("dd") - 5
Dim g1 As String = Date.Now.ToString("yyyy-MM")
Dim g2 As String = Date.Now.ToString("dd") - 6
Dim d1 As String = a1 + "-" + a2
Dim d2 As String = b1 + "-" + b2
Dim d3 As String = c1 + "-" + c2
Dim d4 As String = dd1 + "-" + dd2
Dim d5 As String = e1 + "-" + e2
Dim d6 As String = f1 + "-" + f2
Dim d7 As String = g1 + "-" + g2
Dim lines11 = RichTextBox4.Lines
Dim modifiedLines11 = New List(Of String)()
For index11 As Integer = 0 To lines11.Length - 1
Dim url11 = lines11(index11).Substring(lines11(index11).LastIndexOf(" "c))
modifiedLines11.Insert(index11, lines11(index11).Replace(url11, String.Empty))
modifiedLines11.Add(url11)
RichTextBox4.Text = String.Join(Environment.NewLine, modifiedLines11)
Next
' after split to 2 lists in string
Dim o1 As String = RichTextBox4.Lines(0) ' The first lines of the string is the dates
Dim o2 As String = RichTextBox4.Lines(1)
Dim o3 As String = RichTextBox4.Lines(2)
Dim o4 As String = RichTextBox4.Lines(3)
Dim o5 As String = RichTextBox4.Lines(4)
Dim o6 As String = RichTextBox4.Lines(5)
Dim o7 As String = RichTextBox4.Lines(6)
Dim l1 As String ' This is the second list in the string those are the times
Dim l2 As String
Dim l3 As String
Dim l4 As String
Dim l5 As String
Dim l6 As String
Dim l7 As String
我在这里尝试将它与实际日期进行比较,如果不是,它会给我 00:00:00。
如果 o1 = d1 那么
l1 = RichTextBox4.Lines(x)
别的
l1 =“00:00:00”
If o2 = d2 Then
l2 = RichTextBox4.Lines(x + 1)
Else
l2 = "00:00:00"
If o3 = d3 Then
l3 = RichTextBox4.Lines(x + 2)
Else
l3 = "00:00:00"
If o4 = d4 Then
l4 = RichTextBox4.Lines(x + 3)
Else
l4 = "00:00:00"
If o5 = o5 Then
l5 = RichTextBox4.Lines(x + 4)
Else
l5 = "00:00:00"
If o6 = d6 Then
l6 = RichTextBox4.Lines(x + 5)
Else
l6 = "00:00:00"
If o7 = d7 Then
l7 = RichTextBox4.Lines(x + 6)
Else
l7 = "00:00:00"
End If
End If
End If
End If
End If
End If
End If
' here im trying to calculate the time.
Dim ts1 As TimeSpan = TimeSpan.Parse(l1)
Dim ts2 As TimeSpan = TimeSpan.Parse(l2)
Dim ts3 As TimeSpan = TimeSpan.Parse(l3)
Dim ts4 As TimeSpan = TimeSpan.Parse(l4)
Dim ts5 As TimeSpan = TimeSpan.Parse(l5)
Dim ts6 As TimeSpan = TimeSpan.Parse(l6)
Dim ts7 As TimeSpan = TimeSpan.Parse(l7)
Dim ttsum As TimeSpan = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7
MsgBox(ttsum.ToString)
在我看来,代码有问题,或者方法不好,或者 if 语句不适合这种方法。
因为我没有看到任何 AM/PM 数据,我假设富文本框中的数据是 24 小时格式。
你的代码很复杂,因为你是在操作字符串而不是处理日期。如果你想比较日期和操作日期,你需要做的第一件事就是把你的字符串变成日期。由于我们不知道要处理多少项,因此我们将使用 List(Of T)
.
Private Function GetListOfDates() As List(Of Date)
Dim lst As New List(Of Date)
Dim provider As CultureInfo = CultureInfo.InvariantCulture
For Each line In RichTextBox1.Lines
' 2021-09-17 03:23:44
lst.Add(Date.ParseExact(line, "yyyy-MM-dd hh:mm:ss", provider))
Next
Return lst
End Function
接下来您需要过滤上周的列表。为了您的目的,一天从 00:00:00 开始到 23:59:59
结束
Private Function FilterListForLastWeek(lst As List(Of Date)) As List(Of Date)
Dim DatesInWeek As New List(Of Date)
'I hardcoded the following date to work with your sample data
Dim LastDayOfWeek = New Date(2021, 9, 24, 23, 59, 59)
'You would use the following code in the real version
'Dim LastDayOfWeek = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day, 23, 59, 59)
Debug.Print(LastDayOfWeek.ToString)
Dim FirstDayOfWeek = LastDayOfWeek.AddDays(-6)
FirstDayOfWeek = New Date(FirstDayOfWeek.Year, FirstDayOfWeek.Month, FirstDayOfWeek.Day, 0, 0, 0)
Debug.Print(FirstDayOfWeek.ToString)
For Each d In lst
If d >= FirstDayOfWeek AndAlso d <= LastDayOfWeek Then
DatesInWeek.Add(d)
End If
Next
Return DatesInWeek
End Function
您似乎正在将日期的时、分、秒部分视为时间跨度。要添加 TimeSpan
,您可以使用 Add
方法。来自富文本框的数据中的最后一个日期(第 17 个)被排除在外,因为它不在一周内(第 18-24 天是 7 天)。消息框显示 1:00:24:17,即 1 天零时 24 分 17 秒。您还可以显示为 TotalHours、TotalMinutes 等。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim AllDates = GetListOfDates()
Dim DatesInWeek = FilterListForLastWeek(AllDates)
Dim TimeSpanSum As TimeSpan
For Each d In DatesInWeek
TimeSpanSum = TimeSpanSum.Add(New TimeSpan(d.Hour, d.Minute, d.Second))
Next
MessageBox.Show(TimeSpanSum.ToString)
End Sub
我有一个很难的问题。
我有一个像
这样的字符串
2021-09-24 00:52:27
2021-09-23 03:46:08
2021-09-22 04:13:03
2021-09-21 04:08:02
2021-09-20 02:13:27
2021-09-19 05:27:38
2021-09-18 03:43:32
2021-09-17 03:23:44
我需要计算上周的时间跨度。
字符串并不总是最后 7 天
所以我做了一个字符串来获取最近 7 天并比较它们,如果一行与日期不匹配,它将是 00:00:00,
在那之前我把绳子分开了。
在此之前,我将 X 声明为字符串中的多行
2021-09-24
2021-09-23
2021-09-22
2021-09-21
2021-09-20
2021-09-19
2021-09-18
2021-09-17
00:52:27
03:46:08
04:13:03
04:08:02
02:13:27
05:27:38
03:43:32
03:23:44
Dim x As String = RichTextBox4.Lines.Count ' The number of lines with date and hours together
Dim a1 As String = Date.Now.ToString("yyyy-MM") ' Creating dates for the last 7 days
Dim a2 As String = Date.Now.ToString("dd")
Dim b1 As String = Date.Now.ToString("yyyy-MM")
Dim b2 As String = Date.Now.ToString("dd") - 1
Dim c1 As String = Date.Now.ToString("yyyy-MM")
Dim c2 As String = Date.Now.ToString("dd") - 2
Dim dd1 As String = Date.Now.ToString("yyyy-MM")
Dim dd2 As String = Date.Now.ToString("dd") - 3
Dim e1 As String = Date.Now.ToString("yyyy-MM")
Dim e2 As String = Date.Now.ToString("dd") - 4
Dim f1 As String = Date.Now.ToString("yyyy-MM")
Dim f2 As String = Date.Now.ToString("dd") - 5
Dim g1 As String = Date.Now.ToString("yyyy-MM")
Dim g2 As String = Date.Now.ToString("dd") - 6
Dim d1 As String = a1 + "-" + a2
Dim d2 As String = b1 + "-" + b2
Dim d3 As String = c1 + "-" + c2
Dim d4 As String = dd1 + "-" + dd2
Dim d5 As String = e1 + "-" + e2
Dim d6 As String = f1 + "-" + f2
Dim d7 As String = g1 + "-" + g2
Dim lines11 = RichTextBox4.Lines
Dim modifiedLines11 = New List(Of String)()
For index11 As Integer = 0 To lines11.Length - 1
Dim url11 = lines11(index11).Substring(lines11(index11).LastIndexOf(" "c))
modifiedLines11.Insert(index11, lines11(index11).Replace(url11, String.Empty))
modifiedLines11.Add(url11)
RichTextBox4.Text = String.Join(Environment.NewLine, modifiedLines11)
Next
' after split to 2 lists in string
Dim o1 As String = RichTextBox4.Lines(0) ' The first lines of the string is the dates
Dim o2 As String = RichTextBox4.Lines(1)
Dim o3 As String = RichTextBox4.Lines(2)
Dim o4 As String = RichTextBox4.Lines(3)
Dim o5 As String = RichTextBox4.Lines(4)
Dim o6 As String = RichTextBox4.Lines(5)
Dim o7 As String = RichTextBox4.Lines(6)
Dim l1 As String ' This is the second list in the string those are the times
Dim l2 As String
Dim l3 As String
Dim l4 As String
Dim l5 As String
Dim l6 As String
Dim l7 As String
我在这里尝试将它与实际日期进行比较,如果不是,它会给我 00:00:00。 如果 o1 = d1 那么 l1 = RichTextBox4.Lines(x) 别的 l1 =“00:00:00”
If o2 = d2 Then
l2 = RichTextBox4.Lines(x + 1)
Else
l2 = "00:00:00"
If o3 = d3 Then
l3 = RichTextBox4.Lines(x + 2)
Else
l3 = "00:00:00"
If o4 = d4 Then
l4 = RichTextBox4.Lines(x + 3)
Else
l4 = "00:00:00"
If o5 = o5 Then
l5 = RichTextBox4.Lines(x + 4)
Else
l5 = "00:00:00"
If o6 = d6 Then
l6 = RichTextBox4.Lines(x + 5)
Else
l6 = "00:00:00"
If o7 = d7 Then
l7 = RichTextBox4.Lines(x + 6)
Else
l7 = "00:00:00"
End If
End If
End If
End If
End If
End If
End If
' here im trying to calculate the time.
Dim ts1 As TimeSpan = TimeSpan.Parse(l1)
Dim ts2 As TimeSpan = TimeSpan.Parse(l2)
Dim ts3 As TimeSpan = TimeSpan.Parse(l3)
Dim ts4 As TimeSpan = TimeSpan.Parse(l4)
Dim ts5 As TimeSpan = TimeSpan.Parse(l5)
Dim ts6 As TimeSpan = TimeSpan.Parse(l6)
Dim ts7 As TimeSpan = TimeSpan.Parse(l7)
Dim ttsum As TimeSpan = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7
MsgBox(ttsum.ToString)
在我看来,代码有问题,或者方法不好,或者 if 语句不适合这种方法。
因为我没有看到任何 AM/PM 数据,我假设富文本框中的数据是 24 小时格式。
你的代码很复杂,因为你是在操作字符串而不是处理日期。如果你想比较日期和操作日期,你需要做的第一件事就是把你的字符串变成日期。由于我们不知道要处理多少项,因此我们将使用 List(Of T)
.
Private Function GetListOfDates() As List(Of Date)
Dim lst As New List(Of Date)
Dim provider As CultureInfo = CultureInfo.InvariantCulture
For Each line In RichTextBox1.Lines
' 2021-09-17 03:23:44
lst.Add(Date.ParseExact(line, "yyyy-MM-dd hh:mm:ss", provider))
Next
Return lst
End Function
接下来您需要过滤上周的列表。为了您的目的,一天从 00:00:00 开始到 23:59:59
结束Private Function FilterListForLastWeek(lst As List(Of Date)) As List(Of Date)
Dim DatesInWeek As New List(Of Date)
'I hardcoded the following date to work with your sample data
Dim LastDayOfWeek = New Date(2021, 9, 24, 23, 59, 59)
'You would use the following code in the real version
'Dim LastDayOfWeek = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day, 23, 59, 59)
Debug.Print(LastDayOfWeek.ToString)
Dim FirstDayOfWeek = LastDayOfWeek.AddDays(-6)
FirstDayOfWeek = New Date(FirstDayOfWeek.Year, FirstDayOfWeek.Month, FirstDayOfWeek.Day, 0, 0, 0)
Debug.Print(FirstDayOfWeek.ToString)
For Each d In lst
If d >= FirstDayOfWeek AndAlso d <= LastDayOfWeek Then
DatesInWeek.Add(d)
End If
Next
Return DatesInWeek
End Function
您似乎正在将日期的时、分、秒部分视为时间跨度。要添加 TimeSpan
,您可以使用 Add
方法。来自富文本框的数据中的最后一个日期(第 17 个)被排除在外,因为它不在一周内(第 18-24 天是 7 天)。消息框显示 1:00:24:17,即 1 天零时 24 分 17 秒。您还可以显示为 TotalHours、TotalMinutes 等。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim AllDates = GetListOfDates()
Dim DatesInWeek = FilterListForLastWeek(AllDates)
Dim TimeSpanSum As TimeSpan
For Each d In DatesInWeek
TimeSpanSum = TimeSpanSum.Add(New TimeSpan(d.Hour, d.Minute, d.Second))
Next
MessageBox.Show(TimeSpanSum.ToString)
End Sub