13
A
回答
23
根據您的預期輸出目標,jQuery插件Timeago可能是更好的選擇。
這裏有一個對的HtmlHelper創建包含ISO 8601時間戳的<abbr />
元素:
public static MvcHtmlString Timeago(this HtmlHelper helper, DateTime dateTime) {
var tag = new TagBuilder("abbr");
tag.AddCssClass("timeago");
tag.Attributes.Add("title", dateTime.ToString("s") + "Z");
tag.SetInnerText(dateTime.ToString());
return MvcHtmlString.Create(tag.ToString());
}
結合上述輔助輸出與以下JavaScript某處您的網頁上,你會在金錢。
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
18
我正在使用以下擴展方法。不知道這是否是最好的。
public static string ToRelativeDate(this DateTime dateTime)
{
var timeSpan = DateTime.Now - dateTime;
if (timeSpan <= TimeSpan.FromSeconds(60))
return string.Format("{0} seconds ago", timeSpan.Seconds);
if (timeSpan <= TimeSpan.FromMinutes(60))
return timeSpan.Minutes > 1 ? String.Format("about {0} minutes ago", timeSpan.Minutes) : "about a minute ago";
if (timeSpan <= TimeSpan.FromHours(24))
return timeSpan.Hours > 1 ? String.Format("about {0} hours ago", timeSpan.Hours) : "about an hour ago";
if (timeSpan <= TimeSpan.FromDays(30))
return timeSpan.Days > 1 ? String.Format("about {0} days ago", timeSpan.Days) : "yesterday";
if (timeSpan <= TimeSpan.FromDays(365))
return timeSpan.Days > 30 ? String.Format("about {0} months ago", timeSpan.Days/30) : "about a month ago";
return timeSpan.Days > 365 ? String.Format("about {0} years ago", timeSpan.Days/365) : "about a year ago";
}
助手應該是財產以後這樣的:
public MvcHtmlString Timeago(this HtmlHelper helper, DateTime dateTime)
{
return MvcHtmlString.Create(dateTime.ToRelativeDate());
}
希望它能幫助!
+3
小挑逗,你有這個函數的多元化錯誤 – 2013-02-11 17:42:14
相關問題
- 1. asp.net mvc treeview幫手
- 2. ASP.NET MVC HTML幫手庫
- 3. asp.net mvc 2柵格幫手
- 4. html幫手在asp.net中的html.dropdownlist mvc
- 5. 常用功能和幫手ASP.NET MVC
- 6. ASP.NET MVC 3 Video.Flash幫手不工作
- 7. 動態調用ASP.NET MVC全球幫手
- 8. 創建自定義幫手Asp.net MVC 3
- 9. asp.net MVC 3剃鬚刀定製幫手
- 10. ASP.NET MVC:忽略編輯時的字段
- 11. 幫手將html顯示爲字符串asp.net MVC 5
- 12. ASP.NET MVC TextBoxFor幫手渲染爲空時爲空
- 13. ASP.Net MVC 3 EditorTemplate爲日期時間字段錯誤
- 14. 用於字段的ASP.NET MVC內聯幫助程序
- 15. asp.net MVC 4顯示Unicode文本字段
- 16. 在ASP.NET MVC中的靜態字段MVC
- 17. MVC Razor Html幫手
- 18. Asp.net MVC中的數小時段的時間選擇器
- 19. ASP.NET HttpCookie幫手類
- 20. asp.Net新手幫我
- 21. asp.net mvc jquery隱藏字段
- 22. asp.net mvc datannotation唯一字段
- 23. ASP.NET MVC 4貨幣字段
- 24. 時間以前幫助
- 25. ASP.NET MVC - 的時間.mvc路由在II6
- 26. asp.net mvc Html助手隱藏字段行爲奇怪
- 27. Asp.net jTable時間字段類型
- 28. ASP.NET MVC - 在幫手中獲取當前控制器和操作名稱
- 29. 爲MySQL設置當前時間字段
- 30. 一段時間之前,在字符串
很難確定,因爲我沒有親自使用rails或他指的助手,但是基於文檔中的[context](http://bit.ly/h7zYCS),它看起來很相似到ASP.NET MVC中的HtmlHelper。話雖如此,我不相信這是重複的。是的,建議重複中的代碼可以工作,但這純粹是服務器端方法。所需的輸出不需要在服務器端生成,因此我提供的與複本不同的答案仍然適用。 **不要關閉這個問題**。 – 2011-03-14 19:08:32
包括['time_ago_in_words'](http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words)如何運作的細節可能會幫助人們有效回答這個問題。包括你*想要使用它的細節將會*甚至更好*。 – Shog9 2011-03-14 19:23:18
這真的不應該被關閉...... *請投票重新打開。* – 2011-03-15 17:28:58