Click here to Skip to main content
12,699,123 members (24,609 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB SSRS VB.NET
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


thanks in advance
Posted 26-Nov-12 12:51pm
menacy279
Updated 26-Nov-12 13:07pm
v3
Comments
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
thanks for your reply..

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: bad
 
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2017
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