Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have my code below.i have been able to convert into years months and days.so i add them together to find the average.so if i have for example average = 396 my result should be 1 year, 1month and 6days dis is just and example..i want my output to be like this.

VB
Public Function NaturalLength(length  As integer) As Double
               
               'dim d as Date
               dim year As double
               dim month As double
               dim day  As double
               dim sum  as double
           

'if length =0
'Return String.Empty()
'end if

if length >365.25 then
year =length/ 365.25
length =length  Mod 365.25
length =year *365.25
year =cstr(Math.Floor (length/ 365.25))
year=CInt(fix(length))
'length  =(length  Mod 365.25 )
year = length

end if

if length>=31 And length<366 then
month=length/30.4375
length=length  Mod 30.4375
length=month * 30.4375
month=cstr(Math.Floor (length/30.4375))
month=CInt(fix(length))
'length  =(length  Mod 30.4375)
month=length

end if

if length<31 Then
day =length

end if

sum =(year + month +day)

return sum.tostring()

'return sum.tostring(year+"year" +month+"month"+day+"day")

End Function


in my expression text box i have
=code.NaturalLength(Fields!XXX.value)


i need help on this i try different calculation i got an error..my average looks ok now but to convert in years months and days i still not get into it.


thanks in advance
Posted
Comments
Gregory Gadow 26-Nov-12 15:44pm    
I deleted my answer after copying your function and testing it in VS. Your problem seems to be with the algorithm itself.

If you really aren't happy with the TimeSpan[^] object, then you probably want something like:
VB
Public Sub NaturalLength(length As Integer, ByRef years As Integer, ByRef months As Integer, ByRef days As Integer)

  Dim remain As Double
  Dim amount As Double

  remain = CType(length, Double)
  amount = remain / 365.25
  years = Math.Truncate(amount)

  remain = remain - years * 365.25
  amount = remain / 30.4375
  months = Math.Truncate(amount)

  remain = remain - months * 30.4375
  days = Math.Truncate(remain)
End Sub

Sub Main() ' Just a test
  Dim length As Integer = 396
  Dim y, m, d As Integer
  NaturalLength(length, y, m, d)
  Console.WriteLine("Years: {0}, months: {0}, days = {0}", y, m, d)
End Sub


By the way:
Quote:
for example average = 396 my result should be 1 year, 1month and 6days
looks wrong to me.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Nov-12 18:06pm    
Agree, a 5.
--SA
VB
sum =(year + month +day)

return sum.tostring()


What are you doing here? Just adding all these values into a double and returning that? It does not look like it can possibly return what you want as described here:
menacy wrote:
so if i have for example average = 396 my result should be 1 year, 1month and 6days dis is just and example..i want my output to be like this.


Looks like you are close to getting what you want, just think about what you want to do with the year, month, and day values you have calculated.
 
Share this answer
 
v2
Comments
menacy 26-Nov-12 15:39pm    
hi Wizardzz,

thanks for your comment. i have im sorry i forgot to take average for my expression as code.Naturallength(avg(Fields!XXX.value)).As your question above i try to add years,months and days, and able to find the sum and average(nearest integer).. i add another function but it still not correct.please anyone can figure it out to me please:

Public Function GetCustomDate(ByVal days As Int32) As String
Dim objDate1 As DateTime = DateTime.Now()
Dim objDate2 As DateTime = objDate1.AddDays(-1 * days)
'This has to be modified as not correct.
Return "Years = " & (objDate1.Year - objDate2.Year) & " Months = " & (objDate1.Month - objDate2.Month) & " Days = " & (objDate1.Day - objDate2.Day)

End Function

the logic was not correct.if i had 61 days= 0years 2months 0days or 1096Days= 3years,0months,0days..

please help...

thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900