0

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
James Wilson
  • 5,074
  • 16
  • 63
  • 122

2 Answers2

1

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();
});
Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
1

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.

Josh
  • 1,724
  • 13
  • 15