Frage

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
War es hilfreich?

Lösung

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();
});

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top