Anyone can convert to vb.net using if statement.*urgent..have been able to output the right result,if its 2years 0months 0 days, it should include only 2years or 2years 3 months not include 0days...i dont want value if its to be zero(s)(0)
Public Function GetCustomDate(ByVal days As Int32) As String
Dim timeSpan as System.TimeSpan
'timeSpan = objDate2 - objDate1
Dim objDate1 As DateTime = DateTime.Now()
Dim objDate2 As DateTime = objDate1.AddDays(1 * days)
dim result as string =""
Return (objDate1.Year - objDate2.Year)& "Years" &(objDate1.Month - objDate2.Month) &"Months"&(objDate1.Day - objDate2.Day)& " Days"
You could try using an if statement to see if the value is 0 and if not add to a string.
Public Function GetCustomDate(ByVal days As Int32) As String
Dim objDate1 As DateTime = DateTime.Now()
Dim objDate2 As DateTime = objDate1.AddDays(1 * days)
Dim result As String = Nothing
If Not (objDate1.Year - objDate2.Year) = 0 Then
result += (objDate1.Year - objDate2.Year) & " Years "
End If
If Not (objDate1.Month - objDate2.Month) = 0 Then
result += (objDate1.Month - objDate2.Month) & " Months "
End If
If Not (objDate1.Day - objDate2.Day) = 0 Then
result += (objDate1.Day - objDate2.Day) & " Days "
End If
Return result.Trim()
End Function
0 years, 2 months, and 0 days should return: "2 Months"
2 years, 0 months, and 4 days should return: "2 Years 4 Days"
vadz
Member
6 Points
22 Posts
convert to vb.net
Nov 27, 2012 06:30 PM|LINK
hi,
Anyone can convert to vb.net using if statement.*urgent..have been able to output the right result,if its 2years 0months 0 days, it should include only 2years or 2years 3 months not include 0days...i dont want value if its to be zero(s)(0)
Public Function GetCustomDate(ByVal days As Int32) As String Dim timeSpan as System.TimeSpan 'timeSpan = objDate2 - objDate1 Dim objDate1 As DateTime = DateTime.Now() Dim objDate2 As DateTime = objDate1.AddDays(1 * days) dim result as string ="" Return (objDate1.Year - objDate2.Year)& "Years" &(objDate1.Month - objDate2.Month) &"Months"&(objDate1.Day - objDate2.Day)& " Days"thanks
jprochazka
Contributor
4812 Points
733 Posts
Re: convert to vb.net
Nov 27, 2012 07:33 PM|LINK
You could try using an if statement to see if the value is 0 and if not add to a string.
Public Function GetCustomDate(ByVal days As Int32) As String Dim objDate1 As DateTime = DateTime.Now() Dim objDate2 As DateTime = objDate1.AddDays(1 * days) Dim result As String = Nothing If Not (objDate1.Year - objDate2.Year) = 0 Then result += (objDate1.Year - objDate2.Year) & " Years " End If If Not (objDate1.Month - objDate2.Month) = 0 Then result += (objDate1.Month - objDate2.Month) & " Months " End If If Not (objDate1.Day - objDate2.Day) = 0 Then result += (objDate1.Day - objDate2.Day) & " Days " End If Return result.Trim() End Function0 years, 2 months, and 0 days should return: "2 Months"
2 years, 0 months, and 4 days should return: "2 Years 4 Days"
vadz
Member
6 Points
22 Posts
Re: convert to vb.net
Nov 28, 2012 01:48 AM|LINK
hi
i got negative(-ve) values for few of my result.
as i add -1 to my code above i got most positive+value except few still neagtive(-ve). why i got it wrong?
anywayzz thanks a
thanks a million...
jprochazka
Contributor
4812 Points
733 Posts
Re: convert to vb.net
Nov 28, 2012 03:00 PM|LINK
Maybe objDate1 and objDate2 should switch positions?
Not sure if your calculating difference from the past or future hard to tell without knowing.
vadz
Member
6 Points
22 Posts
Re: convert to vb.net
Nov 29, 2012 02:04 AM|LINK
i have to select the 2 dates value...eg 20/04/2009- 13/05/2012..and i have to display who falls on that range..
thanks