TimeSpan Average
-
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
-
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
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.
-
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.
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
-
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
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)
-
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)
Thanks a lot for your kind help. 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