Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. TimeSpan Average

TimeSpan Average

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sumit Domyan
    wrote on last edited by
    #1

    Hi All I am using two TimeSpan varibales, suppose Dim tt As TimeSpan Dim tt1 As TimeSpan Dim start_date As Date = Now.Date Dim end_date As Date = Now.Date.AddMilliseconds(67) Dim max As TimeSpan tt = end_date.Subtract(start_date) end_date = end_date.AddYears(1) tt1 = end_date.Subtract(start_date) Now, i want to calculate the average of tt & tt1. Can anybody tell me how to do it? Thanks Sumit Domyan

    J 1 Reply Last reply
    0
    • S Sumit Domyan

      Hi All I am using two TimeSpan varibales, suppose Dim tt As TimeSpan Dim tt1 As TimeSpan Dim start_date As Date = Now.Date Dim end_date As Date = Now.Date.AddMilliseconds(67) Dim max As TimeSpan tt = end_date.Subtract(start_date) end_date = end_date.AddYears(1) tt1 = end_date.Subtract(start_date) Now, i want to calculate the average of tt & tt1. Can anybody tell me how to do it? Thanks Sumit Domyan

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Im just amazed that someone can understand programming enought to obtain a timespan, but the process of getting an average has eluded them. for future reference, an average is obtained by summing all values and dividing the result by the number of values. ie, with 2 values 20 40 20+40 = 60 60/2 = 30 30 is the average of 20 & 40 So to get the average of your 2 timespan's add them together and divide by 2.

      S 1 Reply Last reply
      0
      • J J4amieC

        Im just amazed that someone can understand programming enought to obtain a timespan, but the process of getting an average has eluded them. for future reference, an average is obtained by summing all values and dividing the result by the number of values. ie, with 2 values 20 40 20+40 = 60 60/2 = 30 30 is the average of 20 & 40 So to get the average of your 2 timespan's add them together and divide by 2.

        S Offline
        S Offline
        Sumit Domyan
        wrote on last edited by
        #3

        Its not like that dear we cannot divide a time span directly. I have written a function for getting timespan average now. You just need to pass a timespan value which will be the sum of two values. Friend Function AverageTimeSpan(ByVal time_span As TimeSpan) As TimeSpan Dim temp_op As Int32 Dim seconds As Int32 Dim milli_seconds As Int32 '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds seconds = 0 If time_span.Days > 0 Then temp_op = time_span.Days seconds += temp_op * 86400 End If If time_span.Hours > 0 Then temp_op = time_span.Hours seconds += temp_op * 3600 End If If time_span.Minutes > 0 Then temp_op = time_span.Minutes seconds += temp_op * 60 End If If time_span.Seconds > 0 Then temp_op = time_span.Seconds seconds += temp_op End If milli_seconds = 0 If seconds > 0 Then If seconds Mod 2 = 0 Then seconds = CInt(seconds / 2) Else milli_seconds = 500 seconds -= 1 seconds = CInt(seconds / 2) End If End If If time_span.Milliseconds > 0 Then temp_op = time_span.Milliseconds temp_op = CInt(temp_op / 2) milli_seconds += temp_op End If AverageTimeSpan = CalculateTimeSpan(seconds, milli_seconds) End Function Friend Function CalculateTimeSpan(ByVal seconds As Integer, ByVal milliseconds As Int32) As TimeSpan Dim days As Int32 Dim hours As Int32 Dim minutes As Int32 Dim temp_time_span As TimeSpan '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds days = CInt(Math.Floor(seconds / 86400)) seconds = seconds Mod 86400 If seconds > 0 Then hours = CInt(Math.Floor(seconds / 3600)) seconds = seconds Mod 3600 Else hours = 0 End If If seconds > 0 Then minutes = CInt(Math.Floor(seconds / 60)) seconds = seconds Mod 60 Else minutes = 0 seconds = 0 End If temp_time_span = New TimeSpan(days, hours, minutes, seconds, milliseconds) CalculateTimeSpan = temp_time_span End Function

        R J 2 Replies Last reply
        0
        • S Sumit Domyan

          Its not like that dear we cannot divide a time span directly. I have written a function for getting timespan average now. You just need to pass a timespan value which will be the sum of two values. Friend Function AverageTimeSpan(ByVal time_span As TimeSpan) As TimeSpan Dim temp_op As Int32 Dim seconds As Int32 Dim milli_seconds As Int32 '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds seconds = 0 If time_span.Days > 0 Then temp_op = time_span.Days seconds += temp_op * 86400 End If If time_span.Hours > 0 Then temp_op = time_span.Hours seconds += temp_op * 3600 End If If time_span.Minutes > 0 Then temp_op = time_span.Minutes seconds += temp_op * 60 End If If time_span.Seconds > 0 Then temp_op = time_span.Seconds seconds += temp_op End If milli_seconds = 0 If seconds > 0 Then If seconds Mod 2 = 0 Then seconds = CInt(seconds / 2) Else milli_seconds = 500 seconds -= 1 seconds = CInt(seconds / 2) End If End If If time_span.Milliseconds > 0 Then temp_op = time_span.Milliseconds temp_op = CInt(temp_op / 2) milli_seconds += temp_op End If AverageTimeSpan = CalculateTimeSpan(seconds, milli_seconds) End Function Friend Function CalculateTimeSpan(ByVal seconds As Integer, ByVal milliseconds As Int32) As TimeSpan Dim days As Int32 Dim hours As Int32 Dim minutes As Int32 Dim temp_time_span As TimeSpan '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds days = CInt(Math.Floor(seconds / 86400)) seconds = seconds Mod 86400 If seconds > 0 Then hours = CInt(Math.Floor(seconds / 3600)) seconds = seconds Mod 3600 Else hours = 0 End If If seconds > 0 Then minutes = CInt(Math.Floor(seconds / 60)) seconds = seconds Mod 60 Else minutes = 0 seconds = 0 End If temp_time_span = New TimeSpan(days, hours, minutes, seconds, milliseconds) CalculateTimeSpan = temp_time_span End Function

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          You are right that the operators are not overloaded. But instead of making such a huge calculation yourself you could just make use of the Ticks property: Dim average As TimeSpan = TimeSpan.FromTicks((tt.Ticks + tt1.Ticks) / 2)

          S 1 Reply Last reply
          0
          • R Robert Rohde

            You are right that the operators are not overloaded. But instead of making such a huge calculation yourself you could just make use of the Ticks property: Dim average As TimeSpan = TimeSpan.FromTicks((tt.Ticks + tt1.Ticks) / 2)

            S Offline
            S Offline
            Sumit Domyan
            wrote on last edited by
            #5

            Thanks a lot for your kind help. Sumit Domyan

            1 Reply Last reply
            0
            • S Sumit Domyan

              Its not like that dear we cannot divide a time span directly. I have written a function for getting timespan average now. You just need to pass a timespan value which will be the sum of two values. Friend Function AverageTimeSpan(ByVal time_span As TimeSpan) As TimeSpan Dim temp_op As Int32 Dim seconds As Int32 Dim milli_seconds As Int32 '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds seconds = 0 If time_span.Days > 0 Then temp_op = time_span.Days seconds += temp_op * 86400 End If If time_span.Hours > 0 Then temp_op = time_span.Hours seconds += temp_op * 3600 End If If time_span.Minutes > 0 Then temp_op = time_span.Minutes seconds += temp_op * 60 End If If time_span.Seconds > 0 Then temp_op = time_span.Seconds seconds += temp_op End If milli_seconds = 0 If seconds > 0 Then If seconds Mod 2 = 0 Then seconds = CInt(seconds / 2) Else milli_seconds = 500 seconds -= 1 seconds = CInt(seconds / 2) End If End If If time_span.Milliseconds > 0 Then temp_op = time_span.Milliseconds temp_op = CInt(temp_op / 2) milli_seconds += temp_op End If AverageTimeSpan = CalculateTimeSpan(seconds, milli_seconds) End Function Friend Function CalculateTimeSpan(ByVal seconds As Integer, ByVal milliseconds As Int32) As TimeSpan Dim days As Int32 Dim hours As Int32 Dim minutes As Int32 Dim temp_time_span As TimeSpan '1 Day = 86400 Seconds '1 Hour = 3600 Seconds '1 Minute = 60 Seconds days = CInt(Math.Floor(seconds / 86400)) seconds = seconds Mod 86400 If seconds > 0 Then hours = CInt(Math.Floor(seconds / 3600)) seconds = seconds Mod 3600 Else hours = 0 End If If seconds > 0 Then minutes = CInt(Math.Floor(seconds / 60)) seconds = seconds Mod 60 Else minutes = 0 seconds = 0 End If temp_time_span = New TimeSpan(days, hours, minutes, seconds, milliseconds) CalculateTimeSpan = temp_time_span End Function

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              :wtf: Did that method actually work?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups