「timeago」を計算しようとしていますが、コードに矛盾があります。
今日の日付を使用する
2010 年 11 月 7 日
2010 年 9 月 1 日を使用すると、.NET コードと JS コードの両方で「2 か月」と表示されます。
2010 年 8 月 31 日を使用すると、.NET コードでは「3 か月」と表示され、JS コードでは「2 か月」と表示されます。
この不一致は2010 年 8 月 9 日まで続きます。
基本的に、dateDiff は、11 月 7 日の今日の日付に基づいて、8 月 10 日から 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
つまり、私が抱えている問題は根本的なものであり、論理的なものでもあります。
- DateDiff に関して「正しい」コードはどれですか? ( IE: 2 か月と 14 日は 2 か月または 3 か月と見なされますか? )
- それに応じてそれらを整列させる最良の方法は何ですか?