【问题标题】:Relative date/time for classic ASP经典 ASP 的相对日期/时间
【发布时间】:2009-03-31 01:19:22
【问题描述】:

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

例子:

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

【问题讨论】:

    标签: datetime asp-classic vbscript


    【解决方案1】:

    这是我使用的。可以肯定的是,我只是从 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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:
          DateAdd("n", -1, Now) DateAdd("n", -5, Now) DateAdd("h", -1, Now) DateAdd("h", -3, Now) DateAdd("d", -1, Date) DateAdd("d", -1, Date)

          不确定您所说的星期三部分是什么意思。
          能详细点吗?

          【讨论】:

          • 使用 DateDiff 函数比较当前时间和更新日期,如果是这样的话。
          猜你喜欢
          • 1970-01-01
          • 2012-06-15
          • 1970-01-01
          • 1970-01-01
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          • 1970-01-01
          相关资源
          最近更新 更多