castle monorail里自带两个方法,分别是:
public String AlternativeFriendlyFormatFromNow(DateTime date),
public String FriendlyFormatFromNow(DateTime date)
代码如下,希望对你有帮助:)
public String AlternativeFriendlyFormatFromNow(DateTime date)
{
TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
TimeSpan cur = new TimeSpan(date.Ticks);
TimeSpan diff = now.Subtract(cur);
if (diff.TotalHours <= 24)
{
return "Today";
}
else if (diff.TotalHours <= 48)
{
return "Yesterday";
}
else if (diff.TotalDays <= 40)
{
return String.Format("{0} days ago", diff.Days);
}
else
{
return String.Format("{0} months ago", (diff.Days / 30));
}
}
/// <summary>
/// Returns the difference from the
/// specified <c>date</c> the the current date
/// in a friendly string like "1 day ago"
/// <para>
/// TODO: Think about i18n
/// </para>
/// </summary>
/// <param name="date">The date in the past (should be equal or less than now)</param>
/// <returns></returns>
public String FriendlyFormatFromNow(DateTime date)
{
TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
TimeSpan cur = new TimeSpan(date.Ticks);
TimeSpan diff = now.Subtract(cur);
if (diff.TotalSeconds == 0)
{
return "Just now";
}
if (diff.Days == 0)
{
if (diff.Hours == 0)
{
if (diff.Minutes == 0)
{
return String.Format("{0} second{1} ago",
diff.Seconds, diff.Seconds > 1 ? "s" : String.Empty);
}
else
{
return String.Format("{0} minute{1} ago",
diff.Minutes, diff.Minutes > 1 ? "s" : String.Empty);
}
}
else
{
return String.Format("{0} hour{1} ago",
diff.Hours, diff.Hours > 1 ? "s" :
老刘.
|
菜鸟二级
|园豆:350
|
2007-11-30 15:47