我正在尝试计算“timeago”,我的代码中出现了一个不足之处。
使用今天的日期
2010年11月7日
如果我使用 2010年9月1日,那么我的.NET代码和我的JS代码都说“2个月”
如果我使用 2010年8月31日,那么我的.NET代码会说“3个月”而我的JS代码会在“2个月”中说明
这种差异一直持续到 2010年8月9日。
基于今天的11月7日日期,8月10日至8月31日期间,dateDiff基本上是“关闭”。
这是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
所以我遇到的问题既是基础问题,也是后勤问题。
答案 0 :(得分:1)
做了一些假设并且不得不用C#编写,但是这个版本的代码给了我8月31日的2个月和8月9日的3个月
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;
}
我已经更新了代码,我认为你现在有了预期的结果。希望这会有所帮助。
干杯,瓦格纳。