2010-11-08 90 views
0

我正在计算“timeago”,并且我想到了我的代码中的一个缺陷。日期计算差异(JavaScript VS .NET)

用今天的日期

2010年11月7日

如果我使用2010年9月1日然后我的两个.NET代码和我的JS代码说 “2个月”

如果我使用2010年8月31日然后我的.NET代码说“3个月”,我的JS代码说在“2个月”

此差异一直保留到2010年8月9日

基本上DATEDIFF为“关”,从8月10日 - 根据11月7日的当天的日期8月31日

这里是JavaScript的(从“timeago”插件获取)

var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || 
    seconds < 90 && substitute($l.minute, 1) || 
    minutes < 45 && substitute($l.minutes, Math.round(minutes)) || 
    minutes < 90 && substitute($l.hour, 1) || 
    hours < 24 && substitute($l.hours, Math.round(hours)) || 
    hours < 48 && substitute($l.day, 1) || 
    days < 30 && substitute($l.days, Math.floor(days)) || 
    days < 60 && substitute($l.month, 1) || 
    days < 365 && substitute($l.months, Math.floor(days/30)) || 
    years < 2 && substitute($l.year, 1) || 
    substitute($l.years, Math.floor(years)); 

这是我的.NET代码(由我写的)

Public Function ToDuration(ByVal dt As Date?, _ 
           Optional ByVal suffixAgo As String = Nothing) As String 

     If Not dt Is Nothing Then 
      Dim theDate As Date = dt 
      Dim SecondsAppart As Integer = DateDiff(DateInterval.Second, theDate, Now) 
      Dim output As String 
      If SecondsAppart < 86400 Then 
       Select Case SecondsAppart 
        Case Is <= 59 : output = "less than a minute " & suffixAgo 
        Case Is <= 119 : output = "about a minute " & suffixAgo 
        Case Is <= 3599 : output = DateDiff(DateInterval.Minute, theDate, Now) & " minutes " & suffixAgo 
        Case Is <= 7199 : output = "about an hour " & suffixAgo 
        Case Else : output = DateDiff(DateInterval.Hour, theDate, Now) & " hours " & suffixAgo 
       End Select 

      Else 
       Dim DaysAppart As Integer = DateDiff(DateInterval.Day, theDate, Now) 
       Select Case DaysAppart 
        Case Is <= 1 : output = "yesterday" 
        Case Is <= 30 : output = DateDiff(DateInterval.Day, theDate, Now) & " days " & suffixAgo 
        Case Is <= 60 : output = "about a month " & suffixAgo 
        Case Is <= 365 : output = DateDiff(DateInterval.Month, theDate, Now) & " months " & suffixAgo 
        Case Is <= 730 : output = "about a year " & suffixAgo 
        Case Else : output = DateDiff(DateInterval.Year, theDate, Now) & " years " & suffixAgo 
       End Select 
      End If 

      Return output 
     Else 
      Return String.Empty 
     End If 
    End Function 

所以我遇到的问题是一个基本问题以及后勤问题。

  1. 当涉及到DateDiff时,哪个代码是“正确的”? (IE:是2个月还是14天考虑2个月或3?
  2. 什么是最好的方法让他们排队呢?
+0

我要做的一件事就是用<替换<=。你现在编码的方式正在运行,你说的是两个月还在一个月前。与730天相同大约一年。 – 2010-11-08 03:16:00

+0

我想知道,如果选择美国大部分时区的日期从标准时间到标准时间的变化,这是否是巧合。 – kennebec 2010-11-08 03:30:06

+0

哈哈,甚至没有关于DST的事情。不知道它是否适用于此。 – 2010-11-08 03:31:55

回答

1

提出了一些假设,不得不把它写在C#中,但这个版本的代码给了我2个月为8月31日,3个月为8月9日

 public static string ToDuration(DateTime dt, string suffixAgo) 
     { 
      string output; 
      DateTime theDate; 
      if (dt == null) 
      { 
       output = "now"; 
      } 
      else 
      { 
       theDate = dt; 
       TimeSpan DateInterval = DateTime.Now - theDate; 
       int SecondsAppart = Convert.ToInt32(Math.Floor(DateInterval.TotalSeconds)); 
       if ((SecondsAppart < 86400)) 
       { 

        if (SecondsAppart < 59) 
         output = ("less than a minute " + suffixAgo); 
        else if (SecondsAppart < 119) 
         output = ("about a minute " + suffixAgo); 
        else if (SecondsAppart < 3599) 
         output = string.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo); 
        else if (SecondsAppart < 7199) 
         output = "about an hour " + suffixAgo; 
        else 
         output = string.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo); 
       } 
       else 
       { 
        int DaysAppart = Convert.ToInt32(DateInterval.TotalDays); 
        if (DaysAppart <= 1) 
         output = "yesterday"; 
        else if (DaysAppart < 30) 
         output = string.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo); 
        else if (DaysAppart < 60) 
         output = "about a month " + suffixAgo; 
        else if (DaysAppart < 365) 
         output = string.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays/30), suffixAgo); 
        else if (DaysAppart < 730) 
         output = ("about a year " + suffixAgo); 
        else 
         output = string.Format("{0} year {1}", Math.Floor(DateInterval.TotalDays/365), suffixAgo); 
       } 
      } 
      return output; 
     } 

我已经更新的代码和我认为你现在已经得到了预期的结果。希望这会有所帮助。

干杯,瓦格纳。

+0

我认为这更接近,但如果我输入8月15日,我会在JS中获得2个月,在.NET中获得3个月。 – 2010-11-08 04:34:25

+0

PS:我想我喜欢string.Format好得多......不知道为什么我没有想到这一点。 – 2010-11-08 04:34:50

+0

我想我知道了......在if语句中,如果你用Math.Floor替换Convert.ToInt32,你应该得到你期待的结果。这是有道理的,因为它正是js代码所做的。我正在更新代码以反映它。 – 2010-11-08 18:52:23