Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DateTime SSRS VB.NET
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.
 
 
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 26-Nov-12 10:09am
menacy279
Comments
Gregory.Gadow at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you really aren't happy with the TimeSpan[^] object, then you probably want something like:
  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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 26-Nov-12 18:06pm
   
Agree, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
menacy at 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)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Afzaal Ahmad Zeeshan 82
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100