문제

I have this in my controller:

    model.LatestPosts = db.TPGForumPosts.Select(v => v).OrderByDescending(d => 
d.dateCreated).Take(5);

This gives me the 5 latest posts.

I am going to display them on my cshtml page.

Is there an easy way to convert the date to days/months since today?

A cold hard truth by jwilson posted 5 days ago
A cold hard fact by jwilson posted 3 months ago
도움이 되었습니까?

해결책

One option would be to use some jQuery and the timeago plugin: http://timeago.yarp.com/

Your view would need to look something like this:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});

다른 팁

There are a couple of options for this. If you want to go client side then TimeAgo or Moment.js are good options. To do this in .Net you can use NodaTime which is a great date/time library for .NET.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top