Home > Software design >  Split and calculate string vb.net
Split and calculate string vb.net

Time:09-29

I have kind of difficult question. I have a a string like
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
I need to count the timespan of a last week. The thing the string not always will be the last 7 days so i made a string to get the last 7 days and compare them and if a line isnat matching the date it will be 00:00:00, before that i splited the string. before that i declared X as a number of lines in the string
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

here im trying to compare it to the actual date and if isnt it will give me 00:00:00. If o1 = d1 Then l1 = RichTextBox4.Lines(x) Else 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)

In my opinion the porblem with the code is or the method is bad or the if statements arent good with this method.

CodePudding user response:

Since I don't see any AM/PM data, I am assuming the data in the rich text box is in 24 hour format.

Your code is so complicated because you are manipulate strings instead of dealing with dates. If you want to compare dates and manipulate dates, the first thing you need to do is turn your strings into dates. Since we do not know how many items we are dealing with we will use 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

Next you need to filter the list for last week. For your purposes, a day starts as 00:00:00 and ends at 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

It looks like you are considering the hour, minute, second portion of the date as a time span. To add TimeSpan you use the Add method. The last date in the data from the rich text box (the 17th) is excluded since it is not in the week (days 18-24 inclusive is 7 days). The message box displays 1:00:24:17 which is 1 day, zero hours, 24 minutes, and 17 seconds. You can also display as TotalHours, TotalMinutes, etc..

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
  • Related