在C#中使用ASP.NET MVC显示相对日期(例如:20分钟前)的最佳库是什么?
You don't need a library when a simple extension method can do it. This is an extension method that I have used:
public static string TimeAgo(this DateTime date)
{
TimeSpan timeSince = DateTime.Now.Subtract(date);
if (timeSince.TotalMilliseconds < 1) return "not yet";
if (timeSince.TotalMinutes < 1) return "just now";
if (timeSince.TotalMinutes < 2) return "1 minute ago";
if (timeSince.TotalMinutes < 60) return string.Format("{0} minutes ago", timeSince.Minutes);
if (timeSince.TotalMinutes < 120) return "1 hour ago";
if (timeSince.TotalHours < 24) return string.Format("{0} hours ago", timeSince.Hours);
if (timeSince.TotalDays < 2) return "yesterday";
if (timeSince.TotalDays < 7) return string.Format("{0} days ago", timeSince.Days);
if (timeSince.TotalDays < 14) return "last week";
if (timeSince.TotalDays < 21) return "2 weeks ago";
if (timeSince.TotalDays < 28) return "3 weeks ago";
if (timeSince.TotalDays < 60) return "last month";
if (timeSince.TotalDays < 365) return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30));
if (timeSince.TotalDays < 730) return "last year"; //last but not least...
return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365));
}
How about this? But this is jQuery plugin. not c#.
Humanizer is a fantastic library for this. It is on nuget and includes lots of other great conversions for strings and enums in addition to dates.
我不知道为此存在任何已建立的库,但http://tiredblogger.wordpress.com/2008/08/21/creating-twitter-esque-relative-dates-in-c/应该可以帮助您入门。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.