Adding time-VBForums
Results 1 to 7 of 7

Thread: Adding time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    16

    Adding time

    Hello, im trying to add a bunch of different times.

    For example, i want to add the time in Textbox1 with the time in Textbox2
    The time is in there like this 3:48, as in 3 hours 48 minutes.
    I want to add those 2 up, so i get 7 hours and 36 minutes.
    I want that to get posted in Textbox3 as 7:36
    The problem is that i have to add like 20 different times, and will go above 100 hours in total.

  2. #2
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,336

    Re: Adding time

    use TimeSpans:

    Code:
    Dim times As New List(Of TimeSpan)
    times.Add(New TimeSpan(3, 48, 0))
    times.Add(New TimeSpan(15, 30, 0))
    times.Add(New TimeSpan(10, 0, 0))
    
    Dim total As TimeSpan = times.Aggregate(Function(accumulator As TimeSpan, ts As TimeSpan) accumulator.Add(ts))
    MsgBox(String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds))

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    16

    Re: Adding time

    Quote Originally Posted by .paul. View Post
    use TimeSpans:

    Code:
    Dim times As New List(Of TimeSpan)
    times.Add(New TimeSpan(3, 48, 0))
    times.Add(New TimeSpan(15, 30, 0))
    times.Add(New TimeSpan(10, 0, 0))
    
    Dim total As TimeSpan = times.Aggregate(Function(accumulator As TimeSpan, ts As TimeSpan) accumulator.Add(ts))
    MsgBox(String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds))

    Thanks, but how do i make it get the time from lets say Textbox1 and 2, and post it in Textbox3?

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,336

    Re: Adding time

    what is the format in the textboxes?

    assuming your textbox.text is formatted like this, 3:48:00

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ts1 As TimeSpan = TimeSpan.Parse(TextBox1.Text)
            Dim ts2 As TimeSpan = TimeSpan.Parse(TextBox2.Text)
            Dim total As TimeSpan = ts1.Add(ts2)
            TextBox3.Text = String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds)
        End Sub
    
    End Class
    to do something like that with a List(Of TimeSpan):

    Code:
    Public Class Form1
    
        Dim times As New List(Of TimeSpan)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            times.Add(TimeSpan.Parse(TextBox1.Text))
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim total As TimeSpan = times.Aggregate(Function(accumulator As TimeSpan, ts As TimeSpan) accumulator.Add(ts))
            TextBox3.Text = String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds)
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    16

    Re: Adding time

    Quote Originally Posted by .paul. View Post
    what is the format in the textboxes?

    assuming your textbox.text is formatted like this, 3:48:00

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ts1 As TimeSpan = TimeSpan.Parse(TextBox1.Text)
            Dim ts2 As TimeSpan = TimeSpan.Parse(TextBox2.Text)
            Dim total As TimeSpan = ts1.Add(ts2)
            TextBox3.Text = String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds)
        End Sub
    
    End Class
    to do something like that with a List(Of TimeSpan):

    Code:
    Public Class Form1
    
        Dim times As New List(Of TimeSpan)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            times.Add(TimeSpan.Parse(TextBox1.Text))
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim total As TimeSpan = times.Aggregate(Function(accumulator As TimeSpan, ts As TimeSpan) accumulator.Add(ts))
            TextBox3.Text = String.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", total.Days, total.Hours, total.Minutes, total.Seconds)
        End Sub
    
    End Class

    This is exactly what i was looking for, thanks alot!
    One more thing, is there a way i can make the hours go over 24h?
    Last edited by maxlem; Sep 16th, 2013 at 09:01 AM.

  6. #6
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,336

    Re: Adding time

    the timespan has a TotalHours property, as well as days, hours, minutes, seconds

  7. #7
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,336

    Re: Adding time

    it's easier to just multiply the days property value by 24 + add to the hours property value

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.