Skip to main content
added 30 characters in body
Source Link
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal suffixAgo As String = Nothing) As String
        Dim output As String
        Dim theDate As DateTime = dt
        Dim DateInterval As TimeSpan = DateTime.UtcNow - theDate
        Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
        If (SecondsAppart < 86400) Then
            If SecondsAppart < 59 Then
                output = ("less than a minute " + suffixAgo)
            ElseIf SecondsAppart < 119 Then
                output = ("about a minute " + suffixAgo)
            ElseIf SecondsAppart < 3599 Then
                output = String.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo)
            ElseIf SecondsAppart < 7199 Then
                output = "about an hour " + suffixAgo
            Else
                output = String.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo)
            End If
        Else
            Dim DaysAppart As Integer = Math.Floor(DateInterval.TotalDays)
            If DaysAppart <= 1 Then
                output = "yesterday"
            ElseIf DaysAppart < 30 Then
                output = String.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo)
            ElseIf DaysAppart < 60 Then
                output = "about a month " + suffixAgo
            ElseIf DaysAppart < 365 Then
                output = String.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            ElseIf DaysAppart < 730 Then
                output = ("about a year " + suffixAgo)
            Else
                output = String.Format("{0} years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
            End If
        End If 

            Return output
    End Function
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal suffixAgo As String = Nothing) As String
        Dim output As String
        Dim theDate As DateTime = dt
        Dim DateInterval As TimeSpan = DateTime.UtcNow - theDate
        Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
        If (SecondsAppart < 86400) Then
            If SecondsAppart < 59 Then
                output = ("less than a minute " + suffixAgo)
            ElseIf SecondsAppart < 119 Then
                output = ("about a minute " + suffixAgo)
            ElseIf SecondsAppart < 3599 Then
                output = String.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo)
            ElseIf SecondsAppart < 7199 Then
                output = "about an hour " + suffixAgo
            Else
                output = String.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo)
            End If
        Else
            Dim DaysAppart As Integer = Math.Floor(DateInterval.TotalDays)
            If DaysAppart <= 1 Then
                output = "yesterday"
            ElseIf DaysAppart < 30 Then
                output = String.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo)
            ElseIf DaysAppart < 60 Then
                output = "about a month " + suffixAgo
            ElseIf DaysAppart < 365 Then
                output = String.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            ElseIf DaysAppart < 730 Then
                output = ("about a year " + suffixAgo)
            Else
                output = String.Format("{0} years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
            End If
        End If 

            Return output
    End Function
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal suffixAgo As String = Nothing) As String
        Dim output As String
        Dim theDate As DateTime = dt
        Dim DateInterval As TimeSpan = DateTime.UtcNow - theDate
        Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
        If (SecondsAppart < 86400) Then
            If SecondsAppart < 59 Then
                output = ("less than a minute " + suffixAgo)
            ElseIf SecondsAppart < 119 Then
                output = ("about a minute " + suffixAgo)
            ElseIf SecondsAppart < 3599 Then
                output = String.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo)
            ElseIf SecondsAppart < 7199 Then
                output = "about an hour " + suffixAgo
            Else
                output = String.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo)
            End If
        Else
            Dim DaysAppart As Integer = Math.Floor(DateInterval.TotalDays)
            If DaysAppart <= 1 Then
                output = "yesterday"
            ElseIf DaysAppart < 30 Then
                output = String.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo)
            ElseIf DaysAppart < 60 Then
                output = "about a month " + suffixAgo
            ElseIf DaysAppart < 365 Then
                output = String.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            ElseIf DaysAppart < 730 Then
                output = ("about a year " + suffixAgo)
            Else
                output = String.Format("{0} years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
            End If
        End If 

            Return output
    End Function
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal suffixAgo As String = Nothing) As String
        Dim output As String
        Dim theDate As DateTime = dt
        Dim DateInterval As TimeSpan = DateTime.UtcNow - theDate
        Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
        If (SecondsAppart < 86400) Then
            If SecondsAppart < 59 Then
                output = ("less than a minute " + suffixAgo)
            ElseIf SecondsAppart < 119 Then
                output = ("about a minute " + suffixAgo)
            ElseIf SecondsAppart < 3599 Then
                output = String.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo)
            ElseIf SecondsAppart < 7199 Then
                output = "about an hour " + suffixAgo
            Else
                output = String.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo)
            End If
        Else
            Dim DaysAppart As Integer = Math.Floor(DateInterval.TotalDays)
            If DaysAppart <= 1 Then
                output = "yesterday"
            ElseIf DaysAppart < 30 Then
                output = String.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo)
            ElseIf DaysAppart < 60 Then
                output = "about a month " + suffixAgo
            ElseIf DaysAppart < 365 Then
                output = String.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            ElseIf DaysAppart < 730 Then
                output = ("about a year " + suffixAgo)
            Else
                output = String.Format("{0} years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
            End If
        End If 

            Return output
    End Function
deleted 60 characters in body
Source Link
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event<event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "ago"<"suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal appendsuffixAgo As String = Nothing) As String
        Dim SecondsAppartoutput As Integer = DateDiff(DateInterval.Second, input, Now)String
        Dim isRecenttheDate As BooleanDateTime = Falsedt
        Dim outputDateInterval As String
TimeSpan = DateTime.UtcNow - theDate
    If SecondsAppart < 86400 Then
Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
       Select CaseIf (SecondsAppart
  < 86400) Then
            If CaseSecondsAppart Is< <=59 1Then
 : output = "1 sec " & append : isRecent = True
    output = ("less than a minute " + suffixAgo)
    Case Is <= 59 : output = DateDiff(DateInterval.Second, input,ElseIf Now)SecondsAppart &< "119 secsThen
 " & append : isRecent = True
         output = ("about a minute " + CasesuffixAgo)
 Is <= 119 : output = "1 min " & append : isRecentElseIf =SecondsAppart True
< 3599 Then
              Case Is <=output 3599= :String.Format("{0} outputminutes ={1}", DateDiffMath.Floor(DateInterval.Minute, inputTotalMinutes), NowsuffixAgo) 
 & " mins " & append : isRecent = True
  ElseIf SecondsAppart < 7199 Then
          Case Is <= 7199 :  output = "1"about hran hour " &+ appendsuffixAgo
            Else
    Case Else : output = DateDiff(DateInterval.Hour, input, Now) &    output = String.Format("{0} hrshours {1}", &Math.Floor(DateInterval.TotalHours), appendsuffixAgo)
            End Select
If
        Else
            Dim DaysAppart As Integer = DateDiffMath.Floor(DateInterval.Day, input, NowTotalDays)
            Select CaseIf DaysAppart
  <= 1 Then
             Case Is <= 1 : output = "1 day " & append"yesterday"
                Case Is <= 6 : output = DateDiff(DateInterval.Day, input, Now) & "ElseIf daysDaysAppart "< &30 appendThen
                Case Is <= 13 : output = "1String.Format("{0} weekdays {1}", &Math.Floor(DateInterval.TotalDays), appendsuffixAgo)
               ElseIf CaseDaysAppart Is< <=60 30Then
 : output = DateDiff(DateInterval.Weekday, input, Now) & " weeks " & append
    output = "about a month " + suffixAgo
     Case Is <= 60 : output = "1ElseIf monthDaysAppart "< &365 appendThen
                Case Is <= 365 : output = DateDiff(DateIntervalString.Month, input, Now) & Format("{0} months {1}" & append
, Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            CaseElseIf IsDaysAppart <=< 730 : output = "1 year " & appendThen
                Case Else : output = DateDiff(DateInterval.Year, input, Now) &"about "a yearsyear " &+ appendsuffixAgo)
            End SelectElse
        End If

       output If= isRecentString.Format("{0} Then
years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
       Return "<span class=""recent-date"">" & output &End "</span>"If
        ElseEnd If 

            Return output
        End If
    End Function
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "ago"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal append As String = Nothing) As String
        Dim SecondsAppart As Integer = DateDiff(DateInterval.Second, input, Now)
        Dim isRecent As Boolean = False
        Dim output As String
        If SecondsAppart < 86400 Then
            Select Case SecondsAppart
                 Case Is <= 1 : output = "1 sec " & append : isRecent = True
                Case Is <= 59 : output = DateDiff(DateInterval.Second, input, Now) & " secs " & append : isRecent = True
                Case Is <= 119 : output = "1 min " & append : isRecent = True
                Case Is <= 3599 : output = DateDiff(DateInterval.Minute, input, Now) & " mins " & append : isRecent = True
                Case Is <= 7199 : output = "1 hr " & append
                Case Else : output = DateDiff(DateInterval.Hour, input, Now) & " hrs " & append
            End Select

        Else
            Dim DaysAppart As Integer = DateDiff(DateInterval.Day, input, Now)
            Select Case DaysAppart
                 Case Is <= 1 : output = "1 day " & append
                Case Is <= 6 : output = DateDiff(DateInterval.Day, input, Now) & " days " & append
                Case Is <= 13 : output = "1 week " & append
                Case Is <= 30 : output = DateDiff(DateInterval.Weekday, input, Now) & " weeks " & append
                Case Is <= 60 : output = "1 month " & append
                Case Is <= 365 : output = DateDiff(DateInterval.Month, input, Now) & " months " & append
                Case Is <= 730 : output = "1 year " & append
                Case Else : output = DateDiff(DateInterval.Year, input, Now) & " years " & append
            End Select
        End If

        If isRecent Then
            Return "<span class=""recent-date"">" & output & "</span>"
        Else
            Return output
        End If
    End Function
    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event in UTC time</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "suffixAgo"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal suffixAgo As String = Nothing) As String
        Dim output As String
        Dim theDate As DateTime = dt
        Dim DateInterval As TimeSpan = DateTime.UtcNow - theDate
        Dim SecondsAppart As Integer = Math.Floor(DateInterval.TotalSeconds)
        If (SecondsAppart < 86400) Then
            If SecondsAppart < 59 Then
                output = ("less than a minute " + suffixAgo)
            ElseIf SecondsAppart < 119 Then
                output = ("about a minute " + suffixAgo)
            ElseIf SecondsAppart < 3599 Then
                output = String.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo) 
            ElseIf SecondsAppart < 7199 Then
                output = "about an hour " + suffixAgo
            Else
                output = String.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo)
            End If
        Else
            Dim DaysAppart As Integer = Math.Floor(DateInterval.TotalDays)
            If DaysAppart <= 1 Then
                output = "yesterday"
            ElseIf DaysAppart < 30 Then
                output = String.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo)
            ElseIf DaysAppart < 60 Then
                output = "about a month " + suffixAgo
            ElseIf DaysAppart < 365 Then
                output = String.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays / 30), suffixAgo)
            ElseIf DaysAppart < 730 Then
                output = ("about a year " + suffixAgo)
            Else
                output = String.Format("{0} years {1}", Math.Floor(DateInterval.TotalDays / 365), suffixAgo)
            End If
        End If 

            Return output
    End Function
Source Link

Here's a quick and dirty way of how I did mine in .NET

Visual Basic

    ''# <summary>
    ''# Return how long in the past something happend
    ''# </summary>
    ''# <param name="input">The date of an event</param>
    ''# <param name="append">append characters to the end of the string 
    ''# example: "ago"</param>
    ''# <returns>a logical string "10 mins ago"</returns>
    ''# <remarks>This function is primarily used in Views to format Model data.</remarks>
    Public Shared Function GetDateRange(ByVal input As DateTime, Optional ByVal append As String = Nothing) As String
        Dim SecondsAppart As Integer = DateDiff(DateInterval.Second, input, Now)
        Dim isRecent As Boolean = False
        Dim output As String
        If SecondsAppart < 86400 Then
            Select Case SecondsAppart
                Case Is <= 1 : output = "1 sec " & append : isRecent = True
                Case Is <= 59 : output = DateDiff(DateInterval.Second, input, Now) & " secs " & append : isRecent = True
                Case Is <= 119 : output = "1 min " & append : isRecent = True
                Case Is <= 3599 : output = DateDiff(DateInterval.Minute, input, Now) & " mins " & append : isRecent = True
                Case Is <= 7199 : output = "1 hr " & append
                Case Else : output = DateDiff(DateInterval.Hour, input, Now) & " hrs " & append
            End Select

        Else
            Dim DaysAppart As Integer = DateDiff(DateInterval.Day, input, Now)
            Select Case DaysAppart
                Case Is <= 1 : output = "1 day " & append
                Case Is <= 6 : output = DateDiff(DateInterval.Day, input, Now) & " days " & append
                Case Is <= 13 : output = "1 week " & append
                Case Is <= 30 : output = DateDiff(DateInterval.Weekday, input, Now) & " weeks " & append
                Case Is <= 60 : output = "1 month " & append
                Case Is <= 365 : output = DateDiff(DateInterval.Month, input, Now) & " months " & append
                Case Is <= 730 : output = "1 year " & append
                Case Else : output = DateDiff(DateInterval.Year, input, Now) & " years " & append
            End Select
        End If

        If isRecent Then
            Return "<span class=""recent-date"">" & output & "</span>"
        Else
            Return output
        End If
    End Function