views:

1249

answers:

5

Does anyone have a relative date/time from now to a natural/human for classic ASP function in VBScript? This is like Twitter.

Examples:

  • Less than 1 minute ago
  • About 5 minutes ago
  • About an hour ago
  • About 3 hours ago
  • Yesterday
  • Wednesday
  • etc.
A: 

DateAdd("n", -1, Now)
DateAdd("n", -5, Now)
DateAdd("h", -1, Now)
DateAdd("h", -3, Now)
DateAdd("d", -1, Date)
DateAdd("d", -1, Date)

Not sure about what you mean by Wednesday part.
Can you elaborate?

shahkalpesh
Use DateDiff function to compare the current time with Date of Update, if that is the case.
shahkalpesh
e.g. DateDiff("n", dateofupdate ,now)
shahkalpesh
A: 

I wrote my own.

Daniel A. White
+1  A: 

This is the one I use. Pretty certain I just ripped it from Jeff's example that he used for this site.

Yes, yes I did: How can I calculate relative time in C#?

Function RelativeTime(dt)
    Dim t_SECOND : t_SECOND = 1
    Dim t_MINUTE : t_MINUTE = 60 * t_SECOND
    Dim t_HOUR : t_HOUR = 60 * t_MINUTE
    Dim t_DAY : t_DAY = 24 * t_HOUR
    Dim t_MONTH : t_MONTH = 30 * t_DAY

    Dim delta : delta = DateDiff("s", dt, Now)

    Dim strTime : strTime = ""
    If (delta < 1 * t_MINUTE) Then
     If delta = 0 Then
      strTime = "just now"
     ElseIf delta = 1 Then
      strTime = "one second ago"
     Else
      strTime = delta & " seconds ago"
     End If
    ElseIf (delta < 2 * t_MINUTE) Then
      strTime = "a minute ago"
    ElseIf (delta < 50 * t_MINUTE) Then
      strTime = Max(Round(delta / t_MINUTE), 2) & " minutes ago"
    ElseIf (delta < 90 * t_MINUTE) Then
      strTime = "an hour ago"
    ElseIf (delta < 24 * t_HOUR) Then
      strTime = Round(delta / t_HOUR) & " hours ago"
    ElseIf (delta < 48 * t_HOUR) Then
      strTime = "yesterday"
    ElseIf (delta < 30 * t_DAY) Then
     strTime = Round(delta / t_DAY) & " days ago"
    ElseIf (delta < 12 * t_MONTH) Then
     Dim months
     months = Round(delta / t_MONTH)
     If months <= 1 Then
      strTime = "one month ago"
     Else
      strTime = months & " months ago"
     End If
    Else
     Dim years : years = Round((delta / t_DAY) / 365)
     If years <= 1 Then
        strTime = "one year ago"
     Else
      strTime = years & " years ago"
     End If
    End If
    RelativeTime = strTime
End Function
jammus
A: 

taken from ajaxed

'here comes some global helpers...
public function sayDate(dat, mode, relativNotation)
    if not isDate(dat) then
        sayDate = "unknown"
        exit function
    end if
    if relativNotation then
        diff = dateDiff("s", dat, now())
        if diff <= 10 and diff >= 0 then
            sayDate = "Just now" : exit function
        elseif diff < 60 and diff >= 0 then
            sayDate = diff & " seconds ago" : exit function
        elseif diff = 60 and diff >= 0 then
            sayDate = diff & " minute ago" : exit function
        elseif diff <= 1800 and diff >= 0 then
            sayDate = int(diff / 60) & " minutes ago" : exit function
        elseif diff < 86400 and diff >= 0 then
            sayDate = plural(int(diff / 60 / 60), "hour", empty) & " ago"
        else
            if datevalue(dat) = date() then
                sayDate = "Today"
            elseif dateValue(dat) = dateAdd("d", 1, date()) then
                sayDate = "Tomorrow"
            elseif dateValue(dat) = dateAdd("d", -1, date()) then
                sayDate = "Yesterday"
            end if
        end if
    end if
    if relativNotation and lCase(mode) = "datetime" and isEmpty(sayDate) then
        diff = dateDiff("d", dat, now())
        sayDate = plural(diff, "day", empty) & " ago"
        exit function
    end if

    if isEmpty(sayDate) then
        sayDate = day(dat) & ". " & monthname(month(dat), true)
        if year(dat) <> year(now()) then sayDate = sayDate & " " & year(dat)
    end if

    if lCase(mode) <> "datetime" then exit function
    if uBound(split(dat, " ")) <= 0 then exit function
    'sayDate = sayDate & ", " & str.padLeft(hour(dat), 2, "0") & ":" & str.padLeft(minute(dat), 2, "0")
end function

public function plural(val, singularform, pluralform)
    plural = singularform
    if val <> 1 then plural = pluralform
    if isEmpty(plural) then plural = singularform & "s"
    plural = val & " " & plural
end function
Michal
A: 

I write my own function like this, could be found at http://asp.web.id/asp-classic-relative-date-function.html

it is used conversion asp date to unixtimestamp format and calculate the time margin. it is customizable you could also create relative date for upcoming date using this function.

Ariel