有没有人有想法,如何使用日期/时间功能来编写像facebook / twitter风格的时间: 例如5分钟前 例如10小时前。
有没有人有asp.net的教程..
答案 0 :(得分:2)
如果您愿意在客户端进行格式化时间,那么有一个名为timeago的jQuery插件可以执行此操作。
答案 1 :(得分:2)
public static class DateExtension {
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 == 1)
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));
}
}
最初来自http://www.blog.ingenuitynow.net/Extension+Method+For+DateTime+TimeAgo.aspx
答案 2 :(得分:0)
我在asp.net上实现了类似的代码,我从这个例子开始。
http://www.codeproject.com/KB/datetime/DateDurationCalculation1.aspx