【问题标题】:Returning days/months since latest post返回最新发布后的天数/月数
【发布时间】:2014-03-24 17:49:41
【问题描述】:

我的控制器中有这个:

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

这给了我 5 个最新的帖子。

我将在我的 cshtml 页面上显示它们。

有没有一种简单的方法可以将日期转换为自今天以来的天数/月数?

A cold hard truth by jwilson posted 5 days ago
A cold hard fact by jwilson posted 3 months ago

【问题讨论】:

标签: asp.net-mvc linq razor entity


【解决方案1】:

一种选择是使用一些 jQuery 和 timeago 插件:http://timeago.yarp.com/

您的视图需要如下所示:

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

【讨论】:

    【解决方案2】:

    对此有几个选择。如果你想去客户端,那么 TimeAgo 或Moment.js 是不错的选择。要在 .Net 中执行此操作,您可以使用 NodaTime,它是 .NET 的一个很棒的日期/时间库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多