经典ASP的相对日期/时间

时间:2009-03-31 01:19:23

标签: datetime asp-classic vbscript

对于VBScript中的经典ASP函数,是否有人有从现在到相对日期/时间的自然/人类?这就像推特。

示例:

  • 不到1分钟前
  • 大约5分钟前
  • 大约一小时前
  • 大约3小时前
  • 昨天
  • 星期三

4 个答案:

答案 0 :(得分:3)

这是我使用的那个。很确定我刚从Jeff用来这个网站的例子中删除了它。

是的,我做了: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

答案 1 :(得分:0)

取自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

答案 2 :(得分:0)

我自己编写了这样的函数,可以在http://asp.web.id/asp-classic-relative-date-function.html

找到

它使用转换asp日期到unixtimestamp格式并计算时间余量。它是可自定义的,您还可以使用此功能为即将到来的日期创建相对日期。

答案 3 :(得分:-1)


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

周三部分不确定你的意思。
你能详细说明吗?