2010-11-08 137 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