Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Small method to calculate since time
Message
From
03/10/2011 21:31:53
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Small method to calculate since time
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01525501
Message ID:
01525501
Views:
110
I have a small method which I use to calculate the time since a specific date. Given a date, I need to obtain something as "Awaited since four years, two months, 5 days, 10 hours, 24 minutes and 27 seconds".

Here is the code I have so far but this is too complicated. There has to be a way to simplify that:
        ' Since
        ' expD1 Date
        ' expL1 Since
        ' expC1 Since
        Public Function Since(ByVal tdDate As Date, ByVal tlSince As Boolean, _
         ByVal tcSince As String) As String
            Dim lcHtml As String = ""
            Dim ldDateEquivalentToLastYear As Date = Nothing
            Dim ldDateEquivalentToThisYear As Date = Nothing
            Dim lnDay As Integer = 0
            Dim lnHour As Integer = 0
            Dim lnLocation As Integer = 0
            Dim lnMinute As Integer = 0
            Dim lnMonth As Integer = 0
            Dim lnSecond As Integer = 0
            Dim lnYear As Integer = 0

            lnSecond = DateDiff(DateInterval.Second, tdDate, Date.Now)
            lnMinute = DateDiff(DateInterval.Minute, tdDate, Date.Now)
            lnHour = DateDiff(DateInterval.Hour, tdDate, Date.Now)
            lnDay = DateDiff(DateInterval.Day, tdDate, Date.Now)
            lnMonth = DateDiff(DateInterval.Month, tdDate, Date.Now)
            lnYear = DateDiff(DateInterval.Year, tdDate, Date.Now)

            ' Date equivalent to last year
            ldDateEquivalentToLastYear = New Date(Date.Now.Year - 1, tdDate.Month, tdDate.Day, tdDate.Hour, tdDate.Minute, tdDate.Second)

            ' Date equivalent to this year
            ldDateEquivalentToThisYear = New Date(Date.Now.Year, tdDate.Month, tdDate.Day, tdDate.Hour, tdDate.Minute, tdDate.Second)

            ' If we have a year
            If lnYear > 0 Then
                lcHtml = oApp.GetNumberRepresentationPhrase(lnYear, "year")

                lnMonth = lnMonth - (lnYear * 12)
            End If

            ' If we have a month
            If lnMonth > 0 Then

                ' If we have something
                If lcHtml.Length > 0 Then
                    lcHtml = lcHtml + ", "
                End If

                lcHtml = lcHtml + oApp.GetNumberRepresentationPhrase(lnMonth, "month")
            End If

            ' If we have passed that date
            If Date.Now > ldDateEquivalentToThisYear Then

                ' Add the months
                ldDateEquivalentToThisYear = ldDateEquivalentToThisYear.AddMonths(lnMonth)

                lnDay = DateDiff(DateInterval.Day, ldDateEquivalentToThisYear, Date.Now)
            Else
                lnDay = DateDiff(DateInterval.Day, ldDateEquivalentToLastYear, Date.Now)
            End If

            ' If we have a day
            If lnDay > 0 Then

                ' If we have something
                If lcHtml.Length > 0 Then
                    lcHtml = lcHtml + ", "
                End If

                lcHtml = lcHtml + oApp.GetNumberRepresentationPhrase(lnDay, "day")
            End If

            ' Adjust the hours
            lnHour = lnHour Mod 24

            ' If we have an hour
            If lnHour > 0 Then

                ' If we have something
                If lcHtml.Length > 0 Then
                    lcHtml = lcHtml + ", "
                End If

                lcHtml = lcHtml + oApp.GetNumberRepresentationPhrase(lnHour, "hour")
            End If

            ' Adjust the minutes
            lnMinute = lnMinute Mod 60

            ' If we have a minute
            If lnMinute > 0 Then

                ' If we have something
                If lcHtml.Length > 0 Then
                    lcHtml = lcHtml + ", "
                End If

                lcHtml = lcHtml + oApp.GetNumberRepresentationPhrase(lnMinute, "minute")
            End If

            ' Adjust the seconds
            lnSecond = lnSecond Mod 60

            ' If we have a second
            If lnSecond > 0 Then

                ' If we have something
                If lcHtml.Length > 0 Then
                    lcHtml = lcHtml + ", "
                End If

                lcHtml = lcHtml + oApp.GetNumberRepresentationPhrase(lnSecond, "second")
            End If

            ' Find last comma
            lnlocation = oApp.RAt(",", lcHtml)

            ' If we found it
            If lnLocation > 0 Then
                lcHtml = Mid(lcHtml, 1, lnLocation - 1) + " and" + Mid(lcHtml, lnLocation + 1)
            End If

            ' If we show the since
            If tlSince Then
                lcHtml = tcSince + " " + LCase(lcHtml)
            Else
                lcHtml = UCase(Mid(lcHtml, 1, 1) + Mid(lcHtml, 2))
            End If

            Return lcHtml
        End Function
If anyone would like to recommend a better approach, it would be appreciated.

Thanks
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform