We help IT Professionals succeed at work.

timeago... if function not working

267 Views
Last Modified: 2012-05-11
Public Class timeago
    Shared Function GetDifferenceDate(ByVal date2 As DateTime, ByVal date1 As DateTime) As String
        If DateTime.Compare(date1, date2) >= 0 Then
            Dim ts As TimeSpan = date1.Subtract(date2)
            If String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) > "0 days 0 hours 59 minutes" Then
                Return String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) & " ...0 days 0 hours 59 minutes"
            ElseIf String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) > "0 days 23 hours 59 minutes" Then
                Return "<b>Today</b> at " & date1.ToString("hh:mm tt")
            End If
        End If

        Return date2


    End Function 

Open in new window



for some reason my result shows  0 days 3 hours 45 minutes ...0 days 0 hours 59 minutes

so even tho its been 3 hours ago for some reason it is still returnin the    If String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) > "0 days 0 hours 59 minutes" Then

have i got something wrong in my code ??
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Bit more explanation plz.

Author

Commented:
Hey CodeCruiser,

Ok what im trying to do is get the time of the post and display a message saying either 59 > min ago or today or yesterday

to do this i use timeago.GetDifferenceDate(Res.Result3.Split("|")(2), DateAndTime.Now)
Public Class timeago
    Shared Function GetDifferenceDate(ByVal date2 As DateTime, ByVal date1 As DateTime) As String
        If DateTime.Compare(date1, date2) >= 0 Then
            Dim ts As TimeSpan = date1.Subtract(date2)
            If String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) > "0 days 0 hours 59 minutes" Then
                Return String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) & " ...0 days 0 hours 59 minutes"
            ElseIf String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) > "0 days 23 hours 59 minutes" Then
                Return "<b>Today</b> at " & date1.ToString("hh:mm tt")
            End If
        End If

        Return date2


    End Function 

Open in new window


but for some reason it ONLY shows  Return String.Format("{0} days {1} hours {2} minutes", ts.Days, ts.Hours, ts.Minutes) & " ...0 days 0 hours 59 minutes" even through it has been 3 hours since the last post


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
It is because string comparison is not same as date comparison.

Try
If ts.TotalMinutes <= 59 Then
  ...
Else
   ...
End If

Author

Commented:
But if i do ts.TotalMinutes <= 59 then if its 1hr 48min it will still return it as ts.TotalMinutes is still less than 59
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
school boy error... yes i see that makes sence!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.