가능한 중복 : asp.net MVC에 대한 레일 'time_ago_in_words 도우미와 유사한 아무것도
How do I calculate relative time?asp.net MVC 전에 단어 도우미에서
있습니까?
가능한 중복 : asp.net MVC에 대한 레일 'time_ago_in_words 도우미와 유사한 아무것도
How do I calculate relative time?asp.net MVC 전에 단어 도우미에서
있습니까?
의도 한 출력 대상에 따라 jQuery 플러그인 Timeago이 더 좋은 옵션 일 수 있습니다.
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());
}
이 페이지에 어딘가에 다음과 같은 자바 스크립트 위 헬퍼의 출력을 결합하고 당신이 돈을 수 있습니다 :
은 여기 <abbr />
ISO 8601 타임 스탬프를 포함하는 요소를 생성하는 Html 헬퍼입니다.
<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();
});
현재 다음 확장 방법을 사용하고 있습니다. 그것이 밖으로 사용할 수있는 최고의 하나인지 확실하지 않습니다.
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());
}
는 희망이 도움이!
작은 nitpick, 당신은이 함수로 복수화 오류가 있습니다. –
개인적으로 레일이나 도우미를 사용하지 않았기 때문에 확신하기가 어렵지만 문서의 [컨텍스트] (http://bit.ly/h7zYCS)를 기반으로하면 유사합니다 ASP.NET MVC의 HtmlHelper에 전달합니다. 그것이 말하자면, 나는 이것이 복제본이라고 생각하지 않는다. 예, 제안 된 복제본의 코드는 작동하지만 순전히 서버 측 접근 방식입니다. 원하는 출력을 서버 측에서 생성 할 필요가 없으므로 복제본과 다른 응답을 제공하는 것이 적절합니다. **이 질문을 닫지 마십시오 **. –
['time_ago_in_words'] (http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words)의 작동 방식에 대한 자세한 내용을 포함하면 사람들이이를 효과적으로 지원하는 데 도움이 될 것입니다. * 사용 방법에 대한 세부 정보를 포함하면 더 좋을 것입니다 *. – Shog9
이것은 정말로 폐쇄되어서는 안됩니다 ... * 다시 열어 투표하십시오 * –