Results 1 to 15 of 15

Thread: Elegant code for TimeSince()

Threaded View

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Elegant code for TimeSince()

    I'm writing what I thought was going to be a simple function to describe a time in the past, but it's quickly becoming way more complex than I expected.

    The purpose is for a personal backup routine, so I don't technically "need" this, but it's become a puzzle I'm fascinated by. I'm hoping there's some clever tricks with DateSerial() or whatever to tighten this code up from being a total mess.

    The goal is to be able to go back an arbitrary amount of time, up to years (but not decades) and always return a string with the two most significant values. Sample outputs would be:

    37 seconds ago
    13 minutes, 5 seconds ago
    1 hour, 12 minutes ago
    3 days, 4 hours ago
    2 weeks, 1 day ago
    6 months, 2 weeks ago
    1 year, 2 months ago

    I start by DateDiff()ing the difference in seconds. A long can go up to 68 years worth of seconds, so that's plenty. I also set up a helper function to handle pluralization because that's how I roll:

    Code:
    Private Function Plural(plngNumber As Long, pstrSingular As String) As String
        If plngNumber = 1 Then Plural = "1 " & pstrSingular Else Plural = plngNumber & " " & pstrSingular & "s"
    End Function
    Things started getting weird when I got to "x months, y weeks." Here's what I have so far:

    vb Code:
    1. ' Arbitrary value for "blank" date
    2. Public Const NullDate As Date = #1/27/1991# ' WIDE RIGHT!!!!!
    3.  
    4. Public Function TimeSince(pdtmDate As Date) As String
    5.     Dim lngYears As Long
    6.     Dim lngMonths As Long
    7.     Dim lngWeeks As Long
    8.     Dim lngDays As Long
    9.     Dim lngHours As Long
    10.     Dim lngMinutes As Long
    11.     Dim lngSeconds As Long
    12.     Dim strReturn As String
    13.    
    14.     Do While pdtmDate <> NullDate
    15.         ' Seconds
    16.         lngSeconds = DateDiff("s", pdtmDate, Now())
    17.         If lngSeconds <= 60 Then
    18.             strReturn = Plural(lngSeconds, "second")
    19.             Exit Do
    20.         End If
    21.         ' Minutes, Seconds
    22.         lngMinutes = lngSeconds \ 60
    23.         lngSeconds = lngSeconds - lngMinutes * 60
    24.         If lngMinutes <= 60 Then
    25.             strReturn = Plural(lngMinutes, "minute") & ", " & Plural(lngSeconds, "second")
    26.             Exit Do
    27.         End If
    28.         ' Hours, Minutes
    29.         lngHours = lngMinutes \ 60
    30.         lngMinutes = lngMinutes - lngHours * 60
    31.         If lngHours <= 24 Then
    32.             strReturn = Plural(lngHours, "hour") & ", " & Plural(lngMinutes, "minute")
    33.             Exit Do
    34.         End If
    35.         ' Days, Hours
    36.         lngDays = lngHours \ 24
    37.         lngHours = lngHours - lngDays * 24
    38.         If lngDays <= 7 Then
    39.             strReturn = Plural(lngDays, "day") & ", " & Plural(lngHours, "hour")
    40.             Exit Do
    41.         End If
    42.         ' Weeks, Days
    43.         If pdtmDate >= DateAdd("m", -1, Now()) Then
    44.             lngWeeks = lngDays \ 7
    45.             lngDays = lngDays - lngWeeks * 7
    46.             strReturn = Plural(lngWeeks, "week") & ", " & Plural(lngDays, "day")
    47.             Exit Do
    48.         End If
    49.         ' Months, Weeks
    50.         ' ???
    51.         ' Years, Months
    52.         ' ???
    53.         Exit Do
    54.     Loop
    55.     If Len(strReturn) Then strReturn = strReturn & " ago" Else strReturn = "Never"
    56.     TimeSince = strReturn
    57. End Function
    Last edited by Ellis Dee; Mar 7th, 2016 at 05:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width