12,699,123 members (24,609 online)
Rate this:
See more:
i have my code which runs fine, but i want to use my expression if any or can i do in my expression to select only of years,months and days.example if my output is 3 years=3years and not 3years,0month,0days, and 7months should only display 7 months and not 0years,7months,0days as well as days to be 6days rather than 0 years 0months and 6 days.
here is my code in my expression

`=code.GetCustomDate(avg(Fields!SentenceLength.Value))`

```
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```

Posted 26-Nov-12 12:51pm
menacy279
Updated 26-Nov-12 13:07pm
v3
Sergey Alexandrovich Kryukov 26-Nov-12 18:10pm

What is "select"? You are calculated some time which is "`days` days ago", so what? Did you want to achieve something else.
--SA
menacy 26-Nov-12 18:20pm

hi

i have 2 functions 1 for calculate the range in which i have my Start date and end date.i have been display and find the right Output in average
here is my code

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 >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

on my code above(on my question) i used to display suppose 365 days = 1 year,0 months 0days.which runs fine, as i said i want to display as 1years and not 1 years,0months 0 days.

Rate this:

## Solution 1

Please see my comment to the question: you either formulate it wrong or do it wrong.
All thinkable questions on the topic are answered here:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^]

and also:

http://msdn.microsoft.com/en-us/library/dd992632.aspx[^],
http://msdn.microsoft.com/en-us/library/ee372286.aspx[^],
http://msdn.microsoft.com/en-us/library/ee372287.aspx[^].

Pay attention for the second link: time span and time are different things. If you want to present time span, calculate time span; and your last line doing some operations on the year and month may be conceptually wrong. You can get time span by subtraction operator:

```Dim timeSpan as System.TimeSpan
timeSpan = objDate2 - objDate1; ' as simple as that```

And go from there.

—SA
Rate this:

## Solution 2

you may need to look at the years, months, days and check to see if they are 0 before building a display string.

e.g.

```if iYear > 0 then
sDisp = sDisp & iYear.tostring & "Years "
End if```

Top Experts
Last 24hrsThis month
 OriginalGriff 160 Kornfeld Eliyahu Peter 95 ppolymorphe 90 Gopal Rakhal 45 ProgramFOX 30
 OriginalGriff 3,921 Peter Leow 2,278 ppolymorphe 1,777 Mika Wendelius 1,753 Jochen Arndt 1,479