1

I am using the following function to create a relative DateTime comparison string such as: Today (12 Minutes Ago), or Yesterday (21 Hours Ago), or 3/3/2015 (3 Days Ago).

The function is failing if I have a DateTime comparison between 1 and 2 days, so for example:

If the current time is: 3/6/2015 8:30pm and the comparison time is 3/4/2015 9:00pm

I get: 3/4/2015 (1 Day Ago)

When I should be getting: 3/4/2015 (2 Days Ago).

But what is interesting is that if I have a time comparison of 3/4/2015 7:00pm, it will return 3/4/2015 (2 Days Ago).

What's going on?

Public Function GetRelativeTime(givenDate As DateTime) As String
    If (givenDate.Date = DateTime.Today) Then
        Return "Today " + ConvertTimeSpanToRelativeTime(DateTime.Now.Subtract(givenDate))
    ElseIf (givenDate.Date = DateTime.Today.AddDays(-1)) Then
        Return "Yesterday " + ConvertTimeSpanToRelativeTime(DateTime.Now.Subtract(givenDate))
    Else
        Return givenDate.ToString("d") + " " + ConvertTimeSpanToRelativeTime(DateTime.Now.Subtract(givenDate))
    End If
End Function

Private Shared Function ConvertTimeSpanToRelativeTime(diffDate As TimeSpan) As String
    Dim d As New StringBuilder()
    If diffDate.Days > 0 Then
        d.AppendFormat("({0} {1} ago)", diffDate.Days, If(diffDate.Days > 1, "Days", "Day"))
    ElseIf diffDate.Hours > 0 Then
        d.AppendFormat("({0} {1} ago)", diffDate.Hours, If(diffDate.Hours > 1, "Hours", "Hour"))
    ElseIf diffDate.Minutes > 0 Then
        d.AppendFormat("({0} {1} ago)", diffDate.Minutes, If(diffDate.Minutes > 1, "Minutes", "Minute"))
    ElseIf diffDate.Seconds > 0 Then
        d.AppendFormat("({0} {1} ago)", diffDate.Seconds, If(diffDate.Seconds > 1, "Seconds", "Seconds"))
    ElseIf diffDate.Milliseconds > 0 Then
        d.AppendFormat("(Just Now)", diffDate.Milliseconds)
    End If
    Return d.ToString()
End Function
  • [This seems like](http://stackoverflow.com/a/27904178/1070452) it will work – Ňɏssa Pøngjǣrdenlarp Mar 07 '15 at 01:53
  • Isn't the difference between: `3/6/2015 8:30pm 3/4/2015 9:00pm` 1 day, 23 hours and 30 minutes? (Just under 2 days). And you are only using the day portion which is 1 day. – Steve Wellens Mar 07 '15 at 02:02
  • You know what, that's right ... I guess since I wasn't being granular enough it was confusing me. Can you think of a better way of displaying this, since having a date of 3/4/2015 and 3/6/2015 but still only showing 1 day ago is confusing. –  Mar 07 '15 at 02:21
  • A quick and dirty fix (the best kind) would be say say 'over 1 day ago' or 'between 1 and 2 days ago'. – Steve Wellens Mar 07 '15 at 02:23

1 Answers1

0

I think Steve Wellens nailed it. Basically you're only showing the day portion of 1 day, 23 hours, and 30 minutes. If you really want it to show up as 2 days, then instead of your

DateTime.Now.Subtract(givenDate)

you could use something like

DateDiff(DateInterval.Day, DateTime.Now.Date, givenDate.Date)

I believe this should give you the difference in days as if those two dates were set to midnight, which would be 2 in your case above. Then you could use a DateDiff by hours if this wasn't greater than 0, depending on how you want to play it. I think DateDiff returns a long though so you'll have to adjust your function.

coconaut
  • 166
  • 4
  • That sounds like what I am looking for. Care to share an example with more detail than you've already provided? As I am getting "Value of type 'Long' cannot be converted to 'System.TimeSpan'" –  Mar 07 '15 at 03:03
  • I think I would pass givenDate itself to your function. Then for your days check, you could do If DateDiff(DateInterval.Day, DateTime.Now.Date, givenDate.Date) > 0 Then... (return the formatted string). And then continue with Else If DateDiff(DateInterval.Hours, DateTime.Now, givenDate) > 0... and so forth – coconaut Mar 07 '15 at 03:11