Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
Member_2_5230414

asked on

timeago... if function not working

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 ??
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Bit more explanation plz.
Avatar of Member_2_5230414
Member_2_5230414

ASKER

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


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

Try
If ts.TotalMinutes <= 59 Then
  ...
Else
   ...
End If
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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
school boy error... yes i see that makes sence!